This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from at15/talk/Re-Reika
[talk] Re:Rekia for CMPS 253 Adv. PL
- Loading branch information
Showing
24 changed files
with
1,198 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
|
||
# IDEA | ||
.idea | ||
*.iml | ||
*.iml | ||
.ipynb_checkpoints |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# CMPS253 Re:Reika | ||
|
||
The talk on CMPS 253, Adv. PL as a course project |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env bash | ||
|
||
# get the script path http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself | ||
pushd `dirname $0` > /dev/null | ||
SCRIPTPATH=`pwd -P` | ||
popd > /dev/null | ||
ORIGINAL_WD=${PWD} | ||
cd ${SCRIPTPATH} | ||
|
||
if [ -f "../build/libs/reika-0.0.1-SNAPSHOT-all.jar" ]; then | ||
java -cp ../build/libs/reika-0.0.1-SNAPSHOT-all.jar me.at15.reika.playground.SimpleBinShell | ||
else | ||
echo "Please run 'gradle shadowJar' to build the executable jar" | ||
fi | ||
|
||
cd ${ORIGINAL_WD} |
This file contains 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
30 changes: 30 additions & 0 deletions
30
impl/reika-j/src/main/java/me/at15/reika/playground/AstBuilder.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.at15.reika.playground; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class AstBuilder extends SimpleBinBaseVisitor<Node> { | ||
@Override | ||
public Node visitProg(SimpleBinParser.ProgContext ctx) { | ||
List<Node> terms = new ArrayList<>(); | ||
for (SimpleBinParser.TermContext tm : ctx.term()) { | ||
terms.add(visit(tm)); | ||
} | ||
return new Node.Prog(terms); | ||
} | ||
|
||
@Override | ||
public Node visitInt(SimpleBinParser.IntContext ctx) { | ||
return new Node.Int(Integer.parseInt(ctx.getText())); | ||
} | ||
|
||
@Override | ||
public Node visitBool(SimpleBinParser.BoolContext ctx) { | ||
return new Node.Bool(Boolean.parseBoolean(ctx.getText())); | ||
} | ||
|
||
@Override | ||
public Node visitBinOp(SimpleBinParser.BinOpContext ctx) { | ||
return new Node.BinOp(ctx.op.getText(), visit(ctx.term(0)), visit(ctx.term(1))); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
impl/reika-j/src/main/java/me/at15/reika/playground/CPrinter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package me.at15.reika.playground; | ||
|
||
public class CPrinter implements Visitor<String> { | ||
@Override | ||
public String visit(Node n) { | ||
return n.accept(this); | ||
} | ||
|
||
@Override | ||
public String visit(Node.Prog n) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("#include <stdio.h>\n#include <stdbool.h>\n\nint main(){\n"); | ||
for (Node tm : n.terms) { | ||
sb.append(" "); | ||
sb.append(visit(tm)); | ||
sb.append("\n"); | ||
} | ||
sb.append("}\n"); | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public String visit(Node.Int n) { | ||
return String.valueOf(n.val); | ||
} | ||
|
||
@Override | ||
public String visit(Node.Bool n) { | ||
return String.valueOf(n.val); | ||
} | ||
|
||
@Override | ||
public String visit(Node.BinOp n) { | ||
String l = visit(n.l); | ||
String r = visit(n.r); | ||
if ((l.contains("true") || l.contains("false")) && | ||
r.contains("true") || r.contains("false")) { | ||
return String.format("if (%s %s %s) {\n printf(\"true\\n\"); \n} else {\n printf(\"false\\n\"); \n}", l, n.op, r); | ||
} | ||
// return String.format("printf(\"\\%d\", %s %s %s);\\n", l, n.op, r); | ||
return "printf(\"%d\\n\"," + l + n.op + r + ");"; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
impl/reika-j/src/main/java/me/at15/reika/playground/Evaluator.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package me.at15.reika.playground; | ||
|
||
public class Evaluator implements Visitor<Object> { | ||
@Override | ||
public Object visit(Node n) { | ||
return n.accept(this); | ||
} | ||
|
||
@Override | ||
public Object visit(Node.Prog n) { | ||
Object res = null; | ||
for (Node tm : n.terms) { | ||
res = visit(tm); | ||
System.out.println(res); | ||
} | ||
return res; | ||
} | ||
|
||
@Override | ||
public Object visit(Node.Int n) { | ||
return n.val; | ||
} | ||
|
||
@Override | ||
public Object visit(Node.Bool n) { | ||
return n.val; | ||
} | ||
|
||
@Override | ||
public Object visit(Node.BinOp n) { | ||
Object l = visit(n.l); | ||
Object r = visit(n.r); | ||
if (l == null || r == null) { | ||
System.out.println("eval result contains null"); | ||
} else if ("+".equals(n.op) || "-".equals(n.op)) { | ||
if (l instanceof Integer && r instanceof Integer) { | ||
return "+".equals(n.op) ? (int) l + (int) r : (int) l - (int) r; | ||
} | ||
System.out.println("invalid add"); | ||
} else if ("&&".equals(n.op)) { | ||
if (l instanceof Boolean && r instanceof Boolean) { | ||
return "&&".equals(n.op) ? (boolean) l && (boolean) r : (boolean) l || (boolean) r; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
impl/reika-j/src/main/java/me/at15/reika/playground/NegativeNumInLexer.g4
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
grammar NegativeNumInLexer; | ||
|
||
// handle negative number in ANLTR | ||
// should not put in lexer | ||
|
||
// -1 + 2; | ||
// - 1 + 2; | ||
prog: (term ';')+ ; | ||
|
||
term | ||
: INT | ||
| term ('*' | '/') term | ||
| term ('+' | '-') term | ||
; | ||
|
||
INT: '-'? [0-9]+; | ||
WS: [ \t\r\n] -> skip; |
17 changes: 17 additions & 0 deletions
17
impl/reika-j/src/main/java/me/at15/reika/playground/NegativeNumberInParser.g4
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
grammar NegativeNumberInParser; | ||
|
||
// handle negative number in ANLTR | ||
// the precedence matters | ||
|
||
prog: (term ';')+ ; | ||
|
||
term | ||
: INT | ||
| '-' term | ||
| term ('*' | '/') term | ||
| term ('+' | '-') term | ||
// | '-' term // try -1 + 2 | ||
; | ||
|
||
INT: [0-9]+; | ||
WS: [ \t\r\n] -> skip; |
21 changes: 21 additions & 0 deletions
21
impl/reika-j/src/main/java/me/at15/reika/playground/NegativeNumberInParser2.g4
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
grammar NegativeNumberInParser2; | ||
|
||
// handle negative number in ANLTR | ||
// avoid inspect unary for optmize literal in AST | ||
|
||
prog: (term ';')+ ; | ||
|
||
literal | ||
: '-'? INT // try -1 + 2 | ||
; | ||
|
||
term | ||
: literal | ||
| '-' term | ||
| term ('*' | '/') term | ||
| term ('+' | '-') term | ||
| '(' term ')' | ||
; | ||
|
||
INT: [0-9]+; | ||
WS: [ \t\r\n] -> skip; |
64 changes: 64 additions & 0 deletions
64
impl/reika-j/src/main/java/me/at15/reika/playground/Node.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package me.at15.reika.playground; | ||
|
||
import java.util.List; | ||
|
||
public abstract class Node { | ||
public abstract <T> T accept(Visitor<? extends T> visitor); | ||
|
||
public static class Prog extends Node { | ||
public List<Node> terms; | ||
|
||
public Prog(List<Node> terms) { | ||
this.terms = terms; | ||
} | ||
|
||
@Override | ||
public <T> T accept(Visitor<? extends T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
} | ||
|
||
public static class Bool extends Node { | ||
public boolean val; | ||
|
||
public Bool(boolean val) { | ||
this.val = val; | ||
} | ||
|
||
@Override | ||
public <T> T accept(Visitor<? extends T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
} | ||
|
||
public static class Int extends Node { | ||
public int val; | ||
|
||
public Int(int val) { | ||
this.val = val; | ||
} | ||
|
||
@Override | ||
public <T> T accept(Visitor<? extends T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
} | ||
|
||
public static class BinOp extends Node { | ||
public String op; | ||
public Node l; | ||
public Node r; | ||
|
||
public BinOp(String op, Node l, Node r) { | ||
this.op = op; | ||
this.l = l; | ||
this.r = r; | ||
} | ||
|
||
@Override | ||
public <T> T accept(Visitor<? extends T> visitor) { | ||
return visitor.visit(this); | ||
} | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
impl/reika-j/src/main/java/me/at15/reika/playground/Printer.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.at15.reika.playground; | ||
|
||
public class Printer implements Visitor<String> { | ||
@Override | ||
public String visit(Node n) { | ||
return n.accept(this); | ||
} | ||
|
||
@Override | ||
public String visit(Node.Prog n) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Node tm : n.terms) { | ||
sb.append(visit(tm)); | ||
sb.append('\n'); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
@Override | ||
public String visit(Node.Int n) { | ||
return String.valueOf(n.val); | ||
} | ||
|
||
@Override | ||
public String visit(Node.Bool n) { | ||
return String.valueOf(n.val); | ||
} | ||
|
||
@Override | ||
public String visit(Node.BinOp n) { | ||
return String.format("(%s %s %s)", visit(n.l), n.op, visit(n.r)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
impl/reika-j/src/main/java/me/at15/reika/playground/SimpleBin.g4
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
grammar SimpleBin; | ||
|
||
prog: (term ';')+; | ||
|
||
term | ||
: INT # Int | ||
| BOOL # Bool | ||
| term op=('&&' | '||') term # BinOp | ||
| term op=('+' | '-') term # BinOp | ||
| '(' term ')' # Brackets | ||
; | ||
|
||
|
||
INT: [0-9]+; | ||
BOOL: 'true' | 'false'; | ||
WS: [ \t\r\n] -> skip; |
17 changes: 17 additions & 0 deletions
17
impl/reika-j/src/main/java/me/at15/reika/playground/SimpleBin.tokens
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
T__0=1 | ||
T__1=2 | ||
T__2=3 | ||
T__3=4 | ||
T__4=5 | ||
T__5=6 | ||
T__6=7 | ||
INT=8 | ||
BOOL=9 | ||
WS=10 | ||
';'=1 | ||
'&&'=2 | ||
'||'=3 | ||
'+'=4 | ||
'-'=5 | ||
'('=6 | ||
')'=7 |
Oops, something went wrong.