A source-to-source compiler that translates my languages, ATLAS, into C source code. I decided to embark on this project to learn more about: what happens when something is compiled, how C++ and C work, and what it means to design a good programming language.
- Lexer (Tokenizer)
- Parser
- Code Emitter
- "Helper" classes
- Variable.h
- optimizer? (Future)
::= <> consists of
{x} <> one or more of x
[x] <> zero or one of x
() <> just grouping
< x > <> one and only one of x
|| <> means or ( like java)
KW <> denotes that the thing is a keyword
TK(S) <> token that we have already defined
nl <> new line
... <> ellipses means pattern
program ::= {statement}
statement ::= KW"WRITE" (expression || TK.StringLiteral) TK.Eos
|| KW."STRING" TK.Identifier TK."=" (TK.StringLiteral || TK.Identifier) TK.Eos
|| KW."INT" TK.Identifier TK."=" (TK.IntLiteral || expression) TK.Eos
|| KW."IF" TKS."){" nl {statement} TKS."};" TK.Eos
|| KW."WHILE" TKS."]{" {statement} TKS"};" TK.Eos
|| KW."INPUT" TK.Identifier TK.Eos
|| KW."FOR" TK."[" KW."INT" TK."]" {statment} TK.EOS
|| TK.identifier TK."=" (TK.StringLiteral || TK.IntLiteral) Tk.Eos
comparison ::= expression {(">" || "<" || ">=" || "<=" || "==" || "!=") expression}
expression ::= term {("+" || "-" term)}
term ::= unary {("*" || "/") unary}
unary ::= ["+" || "-"]primary
primary ::= TK.StringLiteral || TK.identifier || TK.FloatLiteral
TK.Eos = ";" End Of Statement -> EOS
- CC - denotes a comment
- WRITE - standard output function
WRITE "hello world"; CC this will print the given string literal
WRITE a * b + c; CC expressions and variables work as well
- IF - conditional branch
IF [n >= 5]{
WRITE "n is not 3";
};
- ELSE - conditional branch
IF [n == 0]{
RETURN 1;
};
ELSE {
NUM x = n * 10;
NUM temp = @mystery(x,); CC calls function and saves value to temp
RETURN n * temp + 4;
};
- INPUT - takes input from user
INPUT STRING name; CC INPUT <Data Type> <Variable Name>
WHILE - loop type
CC Print all positive numbers less than a given n
INPUT NUM n;
NUM i = 0;
WHILE [ i < n]{
WRITE i;
i = i + 1;
};
- FOR - loop type
CC you can loop for a given amount of times
INPUT NUM x;
FOR [x]{
WRITE "Hello";
};
CC you can also loop for the length of a string
FOR ["Strawberry"]{
WRITE x;
};
- DEFINE - used to define a function
CC DEFINE is followed by the function name
CC within the parameters of the function, local
CC variable names and datatypes are seperated by a colon
CC all local parameters are ended by a comma
CC following the dollar sign, we have the return type
DEFINE factorial (n:NUM,) $ NUM {
CC returns n! factorial
IF [n == 0]{
RETURN 1;
};
ELSE {
NUM x = n - 1;
NUM temp = @factorial(x,);
RETURN n * temp;
};
RETURN 1;
};
- RETURN - returns a values within a function
- NUM - a data type
CC all numbers in ATLAS are assumed to be integers unless otherwise specified
NUM x = 10;
- fl - used to specify a float literal
NUM pi = fl3.14;
- STRING - a data type
STRING name = "Anthony";
- @ - designates a function call
CC here we have our factorial function again
DEFINE factorial (n:NUM,) $ NUM {
CC returns n! factorial
IF [n == 0]{
RETURN 1;
};
ELSE {
NUM x = n - 1;
NUM temp = @factorial(x,); CC recursive call
RETURN n * temp;
};
RETURN 1;
};
WRITE "Please enter a number below";
INPUT NUM in;
NUM answer = @factorial(in,); CC call with variable input
WRITE answer;