{"id":17881871,"url":"https://github.com/chengluyu/pl0","last_synced_at":"2025-04-03T02:12:31.347Z","repository":{"id":70816094,"uuid":"103092133","full_name":"chengluyu/PL0","owner":"chengluyu","description":"My implementation of PL/0 language interpreter, which is for the assignment of my compiler theory course.","archived":false,"fork":false,"pushed_at":"2017-12-24T13:37:21.000Z","size":431,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-08T16:14:35.655Z","etag":null,"topics":["compiler-design","interpreter","parser","pl0","sdu","shandong-university"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chengluyu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-11T05:09:04.000Z","updated_at":"2023-03-02T05:57:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"32c8df66-d9f9-4181-b367-8cf567f09d34","html_url":"https://github.com/chengluyu/PL0","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengluyu%2FPL0","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengluyu%2FPL0/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengluyu%2FPL0/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chengluyu%2FPL0/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chengluyu","download_url":"https://codeload.github.com/chengluyu/PL0/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922248,"owners_count":20855345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler-design","interpreter","parser","pl0","sdu","shandong-university"],"created_at":"2024-10-28T12:44:32.099Z","updated_at":"2025-04-03T02:12:31.329Z","avatar_url":"https://github.com/chengluyu.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/chengluyu/PL0/master/docs/pl0.png\" width=200\u003e\n\u003cbr\u003e\n\u003ci\u003e– Just another PL/0 interpreter.\u003c/i\u003e\n\u003c/p\u003e\n\n\n\n## Features\n\n1. Written in modern C++ and object-oriented paradigm.\n2. Be able to generate versatile ast\n3. Be able to generate picture of the ast \n\n## Build\n\nPL/0 uses cmake as its building toolchains, which is available in all platforms. To build PL/0, first and formost, you should clone this project to your computer.\nThen you can follow the instructions below:\n\n\u003cdetails\u003e\n\u003csummary\u003eFor CLion Users\u003c/summary\u003e\nIf you're using CLion as your C++ IDE, you can load this project directly into CLion by clicking \"Import Project from Sources\" on the startup screen of CLion.\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eFor macOS Users\u003c/summary\u003e\nIf you're using CLion as your C++ IDE, you can load this project directly into CLion by clicking \"Import Project from Sources\" on the startup screen of CLion.\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eFor Linux (or Cygwin) Users \u003c/summary\u003e\nIf you're using Linux (or Cygwin). The best way is using GNU make. Just `cd` into this project and execute following command.\n\n```shell\nmkdir -p cmake-build\ncd cmake-build\ncmake .. -G \"Unix Makefiles\"\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eFor Windows Users\u003c/summary\u003e\n\u003cbr\u003e\nMake sure that the Microsoft C++ compiler is installed with your Visual Studio. Then execute `cmake .` in this project's directory. CMake will generate Visual Studio solution file automatically.\n\u003c/details\u003e\n\n## Usage\n\nTo run the code in `example` directory with PL/0,  just run the executable binary:\n\n```shell\npl0 ./example/prime.txt\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eHow to see the bytecode?\u003c/summary\u003e\nIf you want to see the bytecode generated by interpreter internally, just type the same command with `--show-bytecode`:\n\n```shell\npl0 --show-bytecode ./example/prime.txt\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eHow to visualize the syntax tree?\u003c/summary\u003e\nWhat's more, if you want to visualize the abstract syntax tree, just add `--plot-tree tree.gv` and then use dot to compile generated file into picture:\n\n```shell\npl0 --plot-tree tree.gv ./example/prime.txt\ndot -o tree.png -T png tree.gv\n```\n\nThen open `tree.png` to see the result.\n\n![Visualized Syntax Tree](docs/tree.png)\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eAre there more options?\u003c/summary\u003e\nBuilt binary of PL/0 interpreter has few options:\n\n* `--compile-only`: compile but not run the code\n* `--show-bytecode`: print bytecode after generating the code\n* `--show-ast`: print ast after generating the ast\n* `--plot-tree [output_file]`: save DOT (a graphics description language) into `output_file`, you can generate a picture of the ast by graphviz.\n\u003c/details\u003e\n\n## Specification of Target Machine\n\nIn this section, the target instruction set will be demonstrated. The target runtime environment is a stack-based machine. There are four register and a stack in the target machine.\n\n### Registers and Stack\n\nThe purpose of four registers are listed below:\n\n1. `IP` holds the instruction being executed.\n2. `PC` is program counter.\n3. `SP` refers the top of the stack.\n4. `BP` refers the base of current stack frame.\n\nStack has two purposes: allocate variables and being used as evaluation stack.\n\nStack is composed of many consecutive storage unit. In order to gain a deeper understanding, we can imagine that stack is composed of many stack frames and each stack frame is composed of many storage units.\n\nWhen a procedure is called, a new stack frame will be placed at the top of the stack. Vice versa, the stack frame will be removed when the procedure is returned.\n\nBut what is inside the stack frame? At the bottom of the stack frame, there are three variables: \n\n1. Static link is the `BP` register of the procedure in which this procedure is called.\n2. Dynamic link is the `BP` register of the procedure in which this procedure is declared.\n3. Return address refers to the next instruction when this procedure is returned.\n\nLocal variables which declared in this procedure are located beyond three variables. Those are called static zone. Beyond the static zone there is dynamic zone, which is served as evaluation stack.\n\n### Instruction Set\n\nThe instruction consists of three parts: opcode, level, address. There are seven opcodes, which is listed below.\n\n1. `LIT`: Load a immediate value onto the stack. The level field in instruction is unused. The address field holds the immediate value.\n2. `LOD`: Load a variable onto the stack. The level fields indicates the distance from current stack frame to the stack frame where target variable locates. The address field is the index of target variable.\n3. `STO`: Store the value on the top of the stack to a variable. Two fields acts the same as `LOD`.\n4. `CAL`: Call a procedure. The level fields indicates the distance from current stack frame to the stack frame where the callee procedure is defined. The address field is the address of first instruction in the callee procedure.\n5. `INT`: Allocate some variables at the top of evaluation stack. The level field is unused. The address field is the count of local variables.\n6. `JMP`: Unconditionally jump to the address given in address field. The level field is unused.\n7. `JPC`: If the value at top of evaluation is falsy (i.e. zero), jump to the address given in address field. The level field is unused.\n8. `OPR`: Do the operation decided by the address field.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchengluyu%2Fpl0","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchengluyu%2Fpl0","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchengluyu%2Fpl0/lists"}