Skip to content

Commit 809c367

Browse files
authored
Merge pull request #6 from rameel/cleanup
Clean up and formatting
2 parents 1280bab + c617e17 commit 809c367

24 files changed

Lines changed: 237 additions & 186 deletions

samples/CalcExpr/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This project implements a simple mathematical expression parser.
66

77
```sh
88
start
9-
= sum_expr EOF
9+
= S sum_expr EOF
1010
;
1111

1212
sum_expr
@@ -37,7 +37,7 @@ S
3737
= [ \t\r\n]*
3838
;
3939

40-
EOF:
40+
EOF
4141
= $
4242
;
4343
```

samples/TinyC/Node.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ public abstract record Node
77
public static Node Empty() => new BlockNode([]);
88
public static Node Number(int value) => new NumberNode(value);
99
public static Node Variable(string name) => new VariableNode(name);
10-
public static Node If(Node test, Node ifTrue, Node ifFalse) => new IfNode(test, Wrap(ifTrue), Wrap(ifFalse));
10+
public static Node If(Node test, Node ifTrue, Node ifFalse) => new IfNode(test, Block(ifTrue), Block(ifFalse));
1111
public static Node Ternary(Node test, Node ifTrue, Node ifFalse) => new TernaryNode(test, ifTrue, ifFalse);
12-
public static Node WhileLoop(Node test, Node body) => new WhileLoopNode(test, Wrap(body));
13-
public static Node DoWhileLoop(Node test, Node body) => new DoWhileLoopNode(test, Wrap(body));
12+
public static Node WhileLoop(Node test, Node body) => new WhileLoopNode(test, Block(body));
13+
public static Node DoWhileLoop(Node test, Node body) => new DoWhileLoopNode(test, Block(body));
1414
public static Node Unary(char op, Node operand) => new UnaryNode(op, operand);
1515
public static Node Binary(string op, Node left, Node right) => new BinaryNode(op, left, right);
1616
public static Node Assign(Node variable, Node value) => Binary("=", variable, value);
1717
public static Node Block(IReadOnlyList<Node> statements) => new BlockNode(statements);
18-
private static Node Wrap(Node statement) => statement is BlockNode ? statement : new BlockNode([statement]);
18+
public static Node Block(Node statement) => statement is BlockNode ? statement : new BlockNode([statement]);
1919

2020
public sealed override string ToString()
2121
{

samples/TinyC/README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tiny-C
1+
# Tiny-C
22

33
This project implements a parser for the [Tiny-C](http://www.iro.umontreal.ca/~felipe/IFT2030-Automne2002/Complements/tinyc.c) language, a highly simplified version of `C` designed as an educational tool for learning about compilers.
44

@@ -11,7 +11,7 @@ The main differences from the original `Tiny-C` are:
1111
## Tiny-C Grammar
1212

1313
```sh
14-
start:
14+
start
1515
= S statement EOF
1616
;
1717

@@ -20,10 +20,12 @@ keyword
2020
;
2121

2222
number
23-
= [0-9]+;
23+
= [0-9]+
24+
;
2425

2526
variable
26-
= !keyword [a-zA-Z_][a-zA-Z0-9_]*;
27+
= !keyword [a-zA-Z_][a-zA-Z0-9_]*
28+
;
2729

2830
S
2931
= [ \t\n\r]*
@@ -43,34 +45,34 @@ number_expr
4345

4446
expr
4547
= assigment_expr
48+
/ ternary_expr
4649
;
4750

4851
assigment_expr
4952
= var_expr "=" S expr
50-
/ ternary_expr
5153
;
5254

5355
ternary_expr
54-
= or_expr ("?" S expr ":" S ternary_expr)?
56+
= logical_or_expr ("?" S expr ":" S ternary_expr)?
5557
;
5658

57-
or_expr
58-
= and_expr ("||" S and_expr)*
59+
logical_or_expr
60+
= logical_and_expr ("||" S logical_and_expr)*
5961
;
6062

61-
and_expr
62-
= inclusive_or_expr ("&&" S inclusive_or_expr)*
63+
logical_and_expr
64+
= bitwise_or_expr ("&&" S bitwise_or_expr)*
6365
;
6466

65-
inclusive_or_expr
66-
= exclusive_or_expr ("|" S exclusive_or_expr)*
67+
bitwise_or_expr
68+
= bitwise_xor_expr ("|" S bitwise_xor_expr)*
6769
;
6870

69-
exclusive_or_expr
70-
= binary_and_expr ("^" S binary_and_expr)*
71+
bitwise_xor_expr
72+
= bitwise_and_expr ("^" S bitwise_and_expr)*
7173
;
7274

73-
binary_and_expr
75+
bitwise_and_expr
7476
= eq_expr ("&" S eq_expr)*
7577
;
7678

0 commit comments

Comments
 (0)