Skip to content

Commit

Permalink
updated backend code for level 8 tm 13
Browse files Browse the repository at this point in the history
  • Loading branch information
s2060434 committed Oct 6, 2020
1 parent e801a67 commit 0581cb9
Show file tree
Hide file tree
Showing 8 changed files with 1,318 additions and 261 deletions.
64 changes: 34 additions & 30 deletions grammars/level10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ statement: if_statement elif_statement* else_statement?
| ranged_loop
| assignment _EOL+
| print _EOL+
| echo _EOL+

if_statement: "if" expression ":" _EOL INDENT statement+ DEDENT
elif_statement: "elif" expression ":" _EOL INDENT statement+ DEDENT
else_statement: "else" ":" _EOL INDENT statement+ DEDENT
statement_block: _INDENT statement+ _DEDENT

repeat: "repeat" expression "times" ":" _EOL INDENT statement+ DEDENT
if_statement: "if" expression ":" _EOL statement_block
elif_statement: "elif" expression ":" _EOL statement_block
else_statement: "else" ":" _EOL statement_block

ranged_loop: "for" NAME "in" "range" expression "to" expression ":" _EOL INDENT statement+ DEDENT
repeat: "repeat" expression "times" ":" _EOL statement_block

assignment: NAME "is" expression
| ask
ranged_loop: "for" NAME "in" "range" expression "to" expression ":" _EOL statement_block

assignment: NAME "is" expression
| NAME "is" list
| NAME "is" ask

list: (expression ",")+ expression

?expression: sum
| expression "=" sum -> eq
Expand All @@ -27,37 +31,37 @@ assignment: NAME "is" expression
| expression ">" sum -> gt

?sum: product
| sum "+" product -> addition
| sum "-" product -> substraction
| sum "+" product -> addition
| sum "-" product -> substraction

?product: atom
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: NAME
| INTEGER
| FLOAT
| STRING
| "[" (expression? | (expression ",")+ expression) "]" -> list
| "(" expression? ")"
| NAME "at" atom -> list_access

print: "print" expression+
echo: "echo" expression+
ask: "ask" expression+
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: "+" atom -> unary_plus
| "-" atom -> unary_minus
| NAME
| NUMBER
| STRING
| "(" expression? ")" -> parenthesis
| NAME "at" atom -> list_access

print: "print" atom*
ask: "ask" atom*

_EOL: /(\r?\n[\t ]*)+/

SINGLE_QUOTED_STRING : /'[^']*'/
STRING: SINGLE_QUOTED_STRING | DOUBLE_QUOTED_STRING

%import common.WS_INLINE
%import common.SIGNED_INT -> INTEGER
%import common.NUMBER -> NUMBER
%import common.FLOAT -> FLOAT
%import common.CNAME -> NAME
%import common._STRING_ESC_INNER
%import common.ESCAPED_STRING -> STRING // TODO: support single quoted strings
%import common.ESCAPED_STRING -> DOUBLE_QUOTED_STRING

%declare INDENT DEDENT
%declare _INDENT _DEDENT

%ignore WS_INLINE


64 changes: 34 additions & 30 deletions grammars/level11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ statement: if_statement elif_statement* else_statement?
| ranged_loop
| assignment _EOL+
| print _EOL+
| echo _EOL+

if_statement: "if" expression ":" _EOL INDENT statement+ DEDENT
elif_statement: "elif" expression ":" _EOL INDENT statement+ DEDENT
else_statement: "else" ":" _EOL INDENT statement+ DEDENT
statement_block: _INDENT statement+ _DEDENT

repeat: "repeat" expression "times" ":" _EOL INDENT statement+ DEDENT
if_statement: "if" expression ":" _EOL statement_block
elif_statement: "elif" expression ":" _EOL statement_block
else_statement: "else" ":" _EOL statement_block

ranged_loop: "for" NAME "in" "range" "(" expression "," expression ")" ":" _EOL INDENT statement+ DEDENT
repeat: "repeat" expression "times" ":" _EOL statement_block

assignment: NAME "is" expression
| ask
ranged_loop: "for" NAME "in" "range" "(" expression "," expression ")" ":" _EOL statement_block

assignment: NAME "is" expression
| NAME "is" list
| NAME "is" ask

list: (expression ",")+ expression

?expression: sum
| expression "=" sum -> eq
Expand All @@ -27,37 +31,37 @@ assignment: NAME "is" expression
| expression ">" sum -> gt

?sum: product
| sum "+" product -> addition
| sum "-" product -> substraction
| sum "+" product -> addition
| sum "-" product -> substraction

?product: atom
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: NAME
| INTEGER
| FLOAT
| STRING
| "[" (expression? | (expression ",")+ expression) "]" -> list
| "(" expression? ")"
| NAME "at" atom -> list_access

print: "print" (expression? | (expression ",")+ expression)
echo: "echo" (expression? | (expression ",")+ expression)
ask: "ask" (expression? | (expression ",")+ expression)
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: "+" atom -> unary_plus
| "-" atom -> unary_minus
| NAME
| NUMBER
| STRING
| "(" expression? ")" -> parenthesis
| NAME "at" atom -> list_access

print: "print" "(" [(expression ",")* expression] ")"
ask: "ask" "(" [(expression ",")* expression] ")"

_EOL: /(\r?\n[\t ]*)+/

SINGLE_QUOTED_STRING : /'[^']*'/
STRING: SINGLE_QUOTED_STRING | DOUBLE_QUOTED_STRING

%import common.WS_INLINE
%import common.SIGNED_INT -> INTEGER
%import common.NUMBER -> NUMBER
%import common.FLOAT -> FLOAT
%import common.CNAME -> NAME
%import common._STRING_ESC_INNER
%import common.ESCAPED_STRING -> STRING // TODO: support single quoted strings
%import common.ESCAPED_STRING -> DOUBLE_QUOTED_STRING

%declare INDENT DEDENT
%declare _INDENT _DEDENT

%ignore WS_INLINE


61 changes: 32 additions & 29 deletions grammars/level12.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ statement: if_statement elif_statement* else_statement?
| ranged_loop
| assignment _EOL+
| print _EOL+
| echo _EOL+

if_statement: "if" expression ":" _EOL INDENT statement+ DEDENT
elif_statement: "elif" expression ":" _EOL INDENT statement+ DEDENT
else_statement: "else" ":" _EOL INDENT statement+ DEDENT
statement_block: _INDENT statement+ _DEDENT

repeat: "repeat" expression "times" ":" _EOL INDENT statement+ DEDENT
if_statement: "if" expression ":" _EOL statement_block
elif_statement: "elif" expression ":" _EOL statement_block
else_statement: "else" ":" _EOL statement_block

ranged_loop: "for" NAME "in" "range" "(" expression "," expression ")" ":" _EOL INDENT statement+ DEDENT
repeat: "repeat" expression "times" ":" _EOL statement_block

ranged_loop: "for" NAME "in" "range" "(" expression "," expression ")" ":" _EOL statement_block

assignment: NAME "is" expression
| ask
| NAME "is" ask

?expression: sum
| expression "=" sum -> eq
Expand All @@ -27,37 +28,39 @@ assignment: NAME "is" expression
| expression ">" sum -> gt

?sum: product
| sum "+" product -> addition
| sum "-" product -> substraction
| sum "+" product -> addition
| sum "-" product -> substraction

?product: atom
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: NAME
| INTEGER
| FLOAT
| STRING
| "[" (expression? | (expression ",")+ expression) "]" -> list
| "(" expression? ")"
| NAME "[" expression "]" -> list_access

print: "print" (expression? | (expression ",")+ expression)
echo: "echo" (expression? | (expression ",")+ expression)
ask: "ask" (expression? | (expression ",")+ expression)
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: "+" atom -> unary_plus
| "-" atom -> unary_minus
| NAME
| NUMBER
| STRING
| "[" (expression? | (expression ",")+ expression) "]" -> list
| "(" expression? ")" -> parenthesis
| NAME "[" expression "]" -> list_access

print: "print" "(" [(expression ",")* expression] ")"
ask: "ask" "(" [(expression ",")* expression] ")"

_EOL: /(\r?\n[\t ]*)+/

SINGLE_QUOTED_STRING : /'[^']*'/
STRING: SINGLE_QUOTED_STRING | DOUBLE_QUOTED_STRING

%import common.WS_INLINE
%import common.SIGNED_INT -> INTEGER
%import common.NUMBER -> NUMBER
%import common.FLOAT -> FLOAT
%import common.CNAME -> NAME
%import common._STRING_ESC_INNER
%import common.ESCAPED_STRING -> STRING // TODO: support single quoted strings
%import common.ESCAPED_STRING -> DOUBLE_QUOTED_STRING

%declare INDENT DEDENT

%ignore WS_INLINE
%declare _INDENT _DEDENT

%ignore WS_INLINE

63 changes: 32 additions & 31 deletions grammars/level13.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,60 @@ statement: if_statement elif_statement* else_statement?
| ranged_loop
| assignment _EOL+
| print _EOL+
| echo _EOL+

if_statement: "if" expression ":" _EOL INDENT statement+ DEDENT
elif_statement: "elif" expression ":" _EOL INDENT statement+ DEDENT
else_statement: "else" ":" _EOL INDENT statement+ DEDENT
statement_block: _INDENT statement+ _DEDENT

repeat: "repeat" expression "times" ":" _EOL INDENT statement+ DEDENT
if_statement: "if" expression ":" _EOL statement_block
elif_statement: "elif" expression ":" _EOL statement_block
else_statement: "else" ":" _EOL statement_block

ranged_loop: "for" NAME "in" "range" "(" expression "," expression ")" ":" _EOL INDENT statement+ DEDENT
repeat: "repeat" expression "times" ":" _EOL statement_block

ranged_loop: "for" NAME "in" "range" "(" expression "," expression ")" ":" _EOL statement_block

assignment: NAME "=" expression
| ask
| NAME "=" ask

?expression: sum
| expression "==" sum -> eq
| expression "==" sum -> eq
| expression "!=" sum -> ne
| expression "<=" sum -> le
| expression ">=" sum -> ge
| expression "<" sum -> lt
| expression ">" sum -> gt

?sum: product
| sum "+" product -> addition
| sum "-" product -> substraction
| sum "+" product -> addition
| sum "-" product -> substraction

?product: atom
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: NAME
| INTEGER
| FLOAT
| STRING
| "[" (expression? | (expression ",")+ expression) "]" -> list
| "(" expression? ")"
| NAME "[" expression "]" -> list_access

print: "print" (expression? | (expression ",")+ expression)
echo: "echo" (expression? | (expression ",")+ expression)
ask: "ask" (expression? | (expression ",")+ expression)
| product "*" atom -> multiplication
| product "/" atom -> division
| product "%" atom -> modulo

?atom: "+" atom -> unary_plus
| "-" atom -> unary_minus
| NAME
| NUMBER
| STRING
| "[" (expression? | (expression ",")+ expression) "]" -> list
| "(" expression? ")" -> parenthesis
| NAME "[" expression "]" -> list_access

print: "print" "(" [(expression ",")* expression] ")"
ask: "ask" "(" [(expression ",")* expression] ")"

_EOL: /(\r?\n[\t ]*)+/

SINGLE_QUOTED_STRING : /'[^']*'/
STRING: SINGLE_QUOTED_STRING | DOUBLE_QUOTED_STRING

%import common.WS_INLINE
%import common.SIGNED_INT -> INTEGER
%import common.NUMBER -> NUMBER
%import common.FLOAT -> FLOAT
%import common.CNAME -> NAME
%import common._STRING_ESC_INNER
%import common.ESCAPED_STRING -> STRING // TODO: support single quoted strings
%import common.ESCAPED_STRING -> DOUBLE_QUOTED_STRING

%declare INDENT DEDENT
%declare _INDENT _DEDENT

%ignore WS_INLINE


Loading

0 comments on commit 0581cb9

Please sign in to comment.