Skip to content

ad-world/tinycompiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Compiler

Building a very basic compiler in Go. It will compile BASIC to C code. Using this experience to learn Golang.

Parts

  1. Lexer
  2. Parser
  3. Emitter

Formal Language

  • Language currently undergoing changes, trying to add functions
program -> functions

functions -> function functions
           | main

function -> ident LPAREN param_list RPAREN LBRACE statements RETURN expression RBRACE

main -> MAIN LPAREN RPAREN LBRACE statements RETURN expression RBRACE

param_list -> expression COMMA param_list 
            | expression
            | ε

statements -> statement statements 
            | statement

statement -> PRINT (expression | string)
           | IF comparison THEN statements ENDIF
           | WHILE comparison REPEAT statement ENDWHILE
           | LABEL ident
           | GOTO ident
           | LET ident "=" expression
           | INPUT ident

comparison -> expression (("==" | "!=" | ">" | ">=" | "<" | "<=") expression)+

expression -> term {( "-" | "+" ) term}
            | function_call

term -> unary {( "/" | "*" ) unary}

unary -> ["+" | "-"] primary

primary -> number | ident | function_call

function_call -> ident LPAREN args RPAREN

args -> expression COMMA args
     | expression
     | ε

Usage

This compiler works by compiling your BASIC code -> C code -> Executable. You may use compile.sh which takes an input file and will create the necessary executable.

Use it like this

bash compile.sh {sample}.teeny

Then, check the exec/ folder and you should have an executable named {sample}.

I'm currently working on adding more features to this compiler, such as functions and return types, logical operators, and more.

About

A very small compiler for the BASIC language written in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published