Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #32 from at15/talk/Re-Reika
Browse files Browse the repository at this point in the history
[talk]  Re:Rekia for CMPS 253 Adv. PL
  • Loading branch information
at15 authored Nov 30, 2017
2 parents 3fd8d3d + 038fc64 commit 283ff1e
Show file tree
Hide file tree
Showing 24 changed files with 1,198 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

# IDEA
.idea
*.iml
*.iml
.ipynb_checkpoints
3 changes: 3 additions & 0 deletions doc/talk/cmps253-Re:Reika/README.md
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
99 changes: 99 additions & 0 deletions doc/talk/cmps253-Re:Reika/graph.ipynb

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions impl/reika-j/bin/simplebin
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}
15 changes: 15 additions & 0 deletions impl/reika-j/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.0'
}
}

Expand All @@ -19,6 +21,7 @@ ext.junitPlatformVersion = '1.0.2'
ext.junitJupiterVersion = '5.0.2'
ext.log4jVersion = '2.9.0'

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

Expand Down Expand Up @@ -58,6 +61,7 @@ dependencies {
compile group: 'org.antlr', name: 'antlr4', version: '4.7'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion
compile group: 'org.jline', name: 'jline', version: '3.1.3'

// JUnit Jupiter API and TestEngine implementation
testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
Expand All @@ -78,4 +82,15 @@ task genReika {
'org.antlr.v4.Tool', '-visitor', '-no-listener', '-package', 'me.at15.reika.parser', 'Reika.g4'
}
}
}

task genSimpleBin {
doLast {
exec {
println "Generate parser for Playground SimpleBin"
workingDir "./src/main/java/me/at15/reika/playground"
commandLine 'java', '-cp', "$projectDir/third_party/antlr-4.7-complete.jar", '-Xmx500M',
'org.antlr.v4.Tool', '-visitor', '-no-listener', '-package', 'me.at15.reika.playground', 'SimpleBin.g4'
}
}
}
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 impl/reika-j/src/main/java/me/at15/reika/playground/CPrinter.java
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 impl/reika-j/src/main/java/me/at15/reika/playground/Evaluator.java
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;
}
}
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;
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;
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 impl/reika-j/src/main/java/me/at15/reika/playground/Node.java
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 impl/reika-j/src/main/java/me/at15/reika/playground/Printer.java
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 impl/reika-j/src/main/java/me/at15/reika/playground/SimpleBin.g4
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;
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
Loading

0 comments on commit 283ff1e

Please sign in to comment.