Created
July 22, 2013 20:02
-
-
Save OrangeTide/6057139 to your computer and use it in GitHub Desktop.
Toy scripting language with C-like syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program = _ (DeclareVar | Function { printf("good\n"); } | |
| . { printf("bad\n"); exit(1); } | |
)* | |
| !. { printf("done\n"); exit(0); } | |
DeclareVar = type VarInit ( "," _ VarInit )* ";" _ | |
VarInit = identifier ( "=" _ Expression )? | |
Function = t:type i:identifier "(" _ Parameters? ")" _ Compound { | |
printf("Function:\n"); | |
value_print(t); | |
value_print(i); | |
value_free(t); | |
t = NULL; | |
value_free(i); | |
i = NULL; | |
} | |
Parameter = type identifier | |
Parameters = Parameter ( "," _ Parameter )* | |
Statement = Compound | DeclareVar | IfStmt | WhileStmt | AssignStmt | CallStmt | |
Compound = "{" _ Statement* "}" _ | |
IfStmt = "if" _ "(" _ Expression ")" _ Compound ( "else" _ Compound )? | |
WhileStmt = "while" _ "(" _ Expression ")" _ Compound | |
AssignStmt = identifier ( "[" _ Expression "]" _ )? "=" _ Expression ";" _ | |
CallStmt = v:Var ";" _ | |
Expression = Condition ( ( "<" | ">" | "==" | "<=" | ">=" | "!=" ) _ Condition )? | |
Condition = Term ( ( "+" | "-" | "&" | "|" | "<<" | ">>" ) _ Term )* | |
Term = Factor ( ( "*" | "/" | "%" ) _ Factor )* | |
Var = i:identifier ( "(" _ Arguments? ")" _ )? { | |
printf("Var:\n"); | |
value_print(i); | |
value_free(i); | |
i = NULL; | |
} | |
Factor = number | string | Var | "*" _ Factor | "&" _ identifier | "(" _ Expression ")" _ | |
Arguments = Expression ( "," _ Expression )* | |
type = < "int" | "void" > _ { $$ = type_new(yytext); } | |
number = "0x" < [0-9A-Fa-f]+ > { $$ = number_new(yytext, 16); } | |
| "0b" < [01]+ > { $$ = number_new(yytext, 2); } | |
| "0" < [0-7]+ > { $$ = number_new(yytext, 7); } | |
| < [0-9]+ > _ { $$ = number_new(yytext, 10); } | |
identifier = < [A-Za-z_][A-Za-z0-9_]* > _ { $$ = ident_new(yytext); } | |
string = '"' < [^"]* > '"' _ { $$ = string_new(yytext); } | |
_ = [ \t\r\n]* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment