{"id":16483464,"url":"https://github.com/dbohdan/all-caps-basic","last_synced_at":"2025-07-06T16:32:53.643Z","repository":{"id":68307246,"uuid":"56054247","full_name":"dbohdan/all-caps-basic","owner":"dbohdan","description":"(WIP) A compiler written in Awk","archived":false,"fork":false,"pushed_at":"2016-04-12T11:32:12.000Z","size":69,"stargazers_count":17,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-28T22:42:06.726Z","etag":null,"topics":["awk","compiler","toy-compiler"],"latest_commit_sha":null,"homepage":null,"language":"Awk","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/dbohdan.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":"2016-04-12T10:41:28.000Z","updated_at":"2025-02-06T00:50:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"6809c3a2-9298-442d-8e2e-74624442fd00","html_url":"https://github.com/dbohdan/all-caps-basic","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dbohdan/all-caps-basic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbohdan%2Fall-caps-basic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbohdan%2Fall-caps-basic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbohdan%2Fall-caps-basic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbohdan%2Fall-caps-basic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dbohdan","download_url":"https://codeload.github.com/dbohdan/all-caps-basic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbohdan%2Fall-caps-basic/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263934947,"owners_count":23532229,"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":["awk","compiler","toy-compiler"],"created_at":"2024-10-11T13:14:05.697Z","updated_at":"2025-07-06T16:32:53.615Z","avatar_url":"https://github.com/dbohdan.png","language":"Awk","readme":"# ALL CAPS BASIC\n\nALL CAPS BASIC (tentative name, since it no longer requires the source code to be in all caps) is a compiler written in [Awk](https://en.wikipedia.org/wiki/AWK). It compiles a statically typed, garbage-collected Basic-like programming language to native code through [modern C](https://en.wikipedia.org/wiki/C99). It is intended to be a kind of working model compiler each piece of which is transparent. For this reason all data formats used by the compiler are text-based.\n\n**Warning!** This is **very much** a work in progress and incomplete. Please do **not** submit it to Hacker News, Reddit, etc.\n\nThe source code is compiled in three phases. Because Awk does not support data structures other than (non-nestable) dictionaries no parse tree is built.\n\n| Phase | Component | Input | Output |\n|-------|-----------|-------|--------|\n| Lexical analysis | [lexer.awk](./lexer.awk) | ACB source code | Token file |\n| Parsing | [parser.awk](./parser.awk) | Token file | Intermediate representation (IR) file |\n| Type checking and code generation | [codegen.awk](./codegen.awk) | IR file | C source code |\n\nRun\n\n```shell\n./run.sh -v test/test00.bas\n```\n\nin this repository to see the output at every stage.\n\nThis project was initially inspired by [BaCon](http://basic-converter.org/), Niklaus Wirth's [Oberon-0 \\[pdf\\]](http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf) and the Awk compiler for [Mercat](http://cowlark.com/mercat/).\n\nThe syntax is largely taken from the manuals for Visual Basic 6 and [Gambas](http://gambas.sourceforge.net/). The syntax and the semantics may differ from those two languages, however, both intentionally and not (the developer has hardly used them).\n\n## Sample code\n\n```basic\nsub repeat(s as string, n) as string\n    let result = \"\"\n    for i = 1 to n\n        let result = result \u0026 s\n    end\n    return result\nend\n\nsub main()\n    let s = \"Ha\"\n    print repeat(s, 3)\nend\n```\n\n## Dependencies\n\nACB should work with any POSIX-compatible Awk. It has been tested with nawk, [mawk](http://invisible-island.net/mawk/mawk.html) and [GNU Awk](https://www.gnu.org/software/gawk/). It does not rely on the GNU-specific extensions.\n\nTo produce native binaries you will need a C99-compatible C compiler like GCC or clang.\n\n## Language grammar\n\nThe parser is hand-written, so this may not be accurate. It does not account for comments; those can be thought of as removed at a separate, line-based parsing stage.\n\nBelow `?` means zero or one occurrence, `*` means zero or more occurrences and `/.../` means a regular expression.\n\n\n```\nfile = (sub | sub_declaration)*\nsub = \"SUB\", sub_header, statement_list, \"end\"\nsub_declaration = \"DECLARE\", sub_header\nsub_header = \"SUB\", ident_maybe_type, \"(\", (ident_maybe_type, \",\")*, \")\", (\"AS\", ident)?\nident = /[a-zA-Z0-9][a-zA-Z0-9_]*/\nident_maybe_type = ideat, (\"AS\", ident)?\nstatement_list = nl, ((statement, nl)*, statement)?\nstatement = assignment | \"BREAK\" | conditional | \"CONTINUE\" | declaration | expression | for_loop | return | subcall | while_loop\nassignment = \"LET\", ident, \"=\", expression\nconditional = \"IF\", expression, statement_list, \"END\"\ndeclaration = \"DIM\", ident, \"AS\", ident\nfor_loop = \"FOR\", ident, \"=\", expression, \"TO\", expression, statement_list, \"END\"\nreturn = \"RETURN\", expression\nsubcall = ident, \"(\", expression, (expression, \",\")*, \")\"\nwhile_loop = \"WHILE\", expression, statement_list, \"END\"\nexpression = (\"(\", expression_core, \")\") | expression_core\nexpression_core = ident | (unary_operator, expression) | ((ident | expression), binary_operator, (ident | expression))\nbinary_operator = \"OR\" | \"AND\" | \"=\" | \"\u003c\u003e\" | \"\u003c\" | \"\u003e\" | \"\u003c=\" | \"\u003e=\" | \"+\" | \"-\" | \"*\" | \"/\" | \"%\" | \"\u0026\"\nunary_operator = \"NOT\"\nnl = nl_atom, nl_atom*\nnl_atom = \\n | \":\"\n```\n\n## IR opcodes\n\n### Legend\n\n| Symbol | Meaning |\n|--------|---------|\n| `byref?` | Either the text \"BYREF\" or nothing |\n| `func` | Function identifier |\n| `op[N]` | Either variable identifier or literal |\n| `label` | Label identifier |\n| `n` | Integer literal |\n| `res` | Variable identifier |\n| `type` | Type identifier |\n| `var` | Variable identifier |\n\n### List\n\n```\nARG var type byref?\nDECLARE\nDIM var type\nENDSUB\nINCR var n\nJT/JF label ident\nJMP label\nLABEL label\nPRINT op1 op2 ... opN\nRETTYPE type\nRETURN var\nSET res op1\nSET+ res op1 op2\nSET- res op1 op2\nSET/ res op1 op2\nSET* res op1 op2\nSET% res op1 op2\nSET= res op1 op2\nSET\u003c res op1 op2\nSET\u003e res op1 op2\nSET\u003c\u003e res op1 op2\nSET\u003c= res op1 op2\nSET\u003e= res op1 op2\nSETAND res op1 op2\nSETOR res op1 op2\nSETXOR res op1 op2\nSETAND_NUM res op1 op2\nSETOR_NUM res op1 op2\nSETXOR_NUM res op1 op2\nSETFUNC res func op1 op2 op3 ... opN\nSUB func\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbohdan%2Fall-caps-basic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdbohdan%2Fall-caps-basic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbohdan%2Fall-caps-basic/lists"}