{"id":19339371,"url":"https://github.com/yashmagane/compilers","last_synced_at":"2025-02-24T08:23:02.737Z","repository":{"id":256466258,"uuid":"855392551","full_name":"YashMagane/Compilers","owner":"YashMagane","description":"Compilers Coursework","archived":false,"fork":false,"pushed_at":"2024-09-10T19:47:14.000Z","size":4870,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-06T10:48:24.434Z","etag":null,"topics":["compilers","interpreter","machinecode","risc-v"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/YashMagane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-09-10T19:43:58.000Z","updated_at":"2024-09-10T19:50:21.000Z","dependencies_parsed_at":"2024-09-10T22:43:29.531Z","dependency_job_id":"df268fba-6cc9-40e8-b82f-98c8452c4721","html_url":"https://github.com/YashMagane/Compilers","commit_stats":null,"previous_names":["yashmagane/compilers"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YashMagane%2FCompilers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YashMagane%2FCompilers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YashMagane%2FCompilers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YashMagane%2FCompilers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/YashMagane","download_url":"https://codeload.github.com/YashMagane/Compilers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240442438,"owners_count":19801896,"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":["compilers","interpreter","machinecode","risc-v"],"created_at":"2024-11-10T03:21:48.201Z","updated_at":"2025-02-24T08:23:02.707Z","avatar_url":"https://github.com/YashMagane.png","language":"Java","readme":"This is my coursework for compilers - here are the links to task 1 and 2\nhttps://users.sussex.ac.uk/~hh435/compilers/task1.html\nhttps://users.sussex.ac.uk/~hh435/compilers/task2.html\n\nin case the links break one day -\n\nTask 1:\nCoursework Task 1: Interpreter\nSummary\nIn this task you will write an interpreter for a simple programming language. We will use the parser generator ANTLR as our main tool to reduce the amount of hand-written code required.\n\nThe main resource is a 'skeleton' project that you can download here.\n\nPlease carefully read the details below and the submission guidelines.\n\n\nExecution model and typing rules\nA program in our simple programming language starts execution from a function int main(...), which may occur anywhere in the function declarations. All arguments to functions are passed by value, and the only identifiers defined in any function are as follows:\n\nThe names of other functions (which may be defined before or after the current function);\nThe parameters taken by the current function;\nThe local variables (initialised at the top of the body) of the current function.\nIn other words, all functions are of 'global scope', while parameters and local variables are of 'function scope'. There is one special case: when a local variable is being initialised, all the other local variables are yet defined and thus not available. We do not use an explicit return keyword — the return value of a function is the value of the last expression in its body. We assume that the input program satisfies the following conditions:\n\nThe input program has a main function, which has return type int;\nNo two functions may have the same name;\nThe definition of a function fnA may involve invocations of other functions that are defined before or after the function fnA;\nNo two parameters or local variables of the same function fnA may have the same name;\nNo parameter or local variable may have the same name as a function;\nParameters and local variables of fnA are only in-scope in the body of fnA;\nAll parameters or local variables must have either int or bool types;\nIn addition, we assume it that satisfies the following typing rules:\n\nFor each function, the return type matches the type of the return value;\nFor each function invocation, the numbers and types of the arguments passed match that of the parameters of the function being called;\nThe following expressions have unit type:\nprint x,\nspace,\nnewline,\nskip,\nx := y,\nwhile x do {...},\nrepeat {...} until x;\nThe type of a function invocation f(e1, ..., en) is the return type of f;\nThe type of a block { e1; ...; en } is the type of the last expression en;\nThe type of an if expression is the type of its then block (and its else block);\nIn any comparison such as x \u003c y, both x and y must have int type;\nIn any arithmetic operation such as x + y, both x and y must have int type;\nIn any Boolean operation such as x \u0026 y, both x and y must have bool type;\nIn any if expression, its then block and its else block have the same type;\nIn any if, while, or repeat expression, the condition must have bool type;\nIn any while or repeat expression, the loop body must have unit type;\nIn any assignment x := y, both x and y must have the same type;\nIn any print expression print x, either x is space or newline, or x has int type.\nIn short, the input program is assumed to be 'well-formed' and 'well-typed'. The semantics of the language constructs should be self-explanatory; in particular, your interpreter will print to System.out as requested by print statements in the input program.\n\n\nInput / output specification and some technical details\nYour grammar file is SimpleLang.g4 with a start rule called prog, and thus the lexer and parser classes generated by ANTLR are SimpleLangLexer and SimpleLangParser, respectively.\nYour interpreter class SimpleLangInterpreter implements SimpleLangVisitor\u003cInteger\u003e, and it has an extra method visitProgram which also returns an Integer (return value of main) and takes two arguments: the first is the parse tree's root node (a SimpleLangParser.ProgContext), the second is an array of strings (arguments) where each of them must either be an INTLIT or a BOOLLIT; we assume that they match the number and types of the parameters of main.\nAfter simulating (interpreting) the input program (with the arguments), the interpreter prints a line separator and then two separate lines, each with a line separator: the first contains the string NORMAL_TERMINATION, and the second contains the return value of main.\n\nTask 2:\nCoursework Task 2: RISC-V code generator\nSummary\nIn this task, you are going to implement a code generator targeting RISC-V assembly for the same simple programming language (i.e. you can reuse the lexer and parser developed in Task 1). The principal idea here is to use stack-machine assembly, which can easily be embedded into RISC-V assembly, as an intermediate language to simplify this task.\n\nThe main resource is a 'skeleton' project that you can download here.\n\nPlease carefully read the details below and the submission guidelines.\n\n\nInput / output specification and some technical details\nAs before, we assume that the input program is 'well-formed' and 'well-typed'. Your code generator will generate RISC-V assembly code (or in our case, stack-machine assembly code with RISC-V 'polyfills') that implements the input program and simulate the generated code with RARS. We suggest the following calling convention when calling a function:\n\nPushes a return value onto the stack (meant to be modified by the callee).\nPushes the arguments (in reverse order) onto the stack.\nPushes the return address — the address right after the jump — onto the stack.\nThe organisation of the source files for Task 2 is very similar to Task 1; specifically,\n\nYour code generator class SimpleLangCodeGenerator implements SimpleLangVisitor\u003cString\u003e, and it has an extra method visitProgram which also returns a String (generated code) and takes the same two arguments as in Task 1.\nYour code generator simulates the generated code and prints out the results in exactly the same way as in Task 1 using RARS's API (see the skeleton project for details).\n\nSome suggestions and hints\nEach new assembly instruction should be on a new line — don't put more than one instruction on a single line.\nYou'll need a function that generates fresh labels. Don't hardcode labels, ensure that the function generates a fresh label every time it is called. Don't use a random generator for this, use a global / static variable for this purpose. This is one of the few legitimate uses of global / static variables.\nCheck that your submission is not miscompiling conditional and loop constructs like if / then / else or repeat / until.\nConsider giving a 'dummy' value to the unit expressions like skip (why?)\nWe strongly recommend testing your code before submission. Note that pasting code into RARS and simply press 'Assemble' is insufficient as test, because that doesn't test the termination of the code generated by your code generator. [Run] it!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyashmagane%2Fcompilers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyashmagane%2Fcompilers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyashmagane%2Fcompilers/lists"}