Skip to content

Commit

Permalink
Add basic grammar and Makefile.
Browse files Browse the repository at this point in the history
  • Loading branch information
ks07 committed Jul 6, 2020
0 parents commit 1c222f5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: test clean

PERParser.py :
antlr4 -Dlanguage=Python3 PER.g4

test : export CLASSPATH = /usr/share/java/stringtemplate4.jar:/usr/share/java/antlr4.jar:/usr/share/java/antlr4-runtime.jar:/usr/share/java/antlr3-runtime.jar/:/usr/share/java/treelayout.jar
test :
antlr4 PER.g4 && javac *.java
grun PER per testinput -tokens

clean :
rm -f *.java *.class PERParser.py PERLexer.py PERListener.py *.interp *.tokens
40 changes: 40 additions & 0 deletions PER.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
grammar PER;

/*
* Parser
*/

per : (statement | comment | WHITESPACE)+ EOF ;
statement : OPEN command CLOSE ;
comment : COMMENT ;
command : ( defrule | defconst ) ;
defrule : DEFRULE WHITESPACE+ proposition_list DEFRULE_SEPARATOR WHITESPACE* action_list ;
proposition_list : ( proposition WHITESPACE+ )+ ;
proposition : OPEN SYMBOL ( WHITESPACE (REL_OP | SYMBOL | SHORT) )+ WHITESPACE? CLOSE ;
action_list : ( action WHITESPACE+ )+ ;
action : OPEN SYMBOL ( WHITESPACE (SYMBOL | SHORT | STRING) )+ WHITESPACE? CLOSE ;
defconst : DEFCONST WHITESPACE+ SYMBOL WHITESPACE+ SHORT ;

/*
* Lexer
*/

OPEN : '(' ;
CLOSE : ')' ;
WHITESPACE : ( ' ' | '\t' | NEWLINE ) ;
NEWLINE : ( '\n' | '\r\n' ) ;

COMMENT : ';' ~[\r\n]* NEWLINE ;

DEFRULE : 'defrule' ;
DEFRULE_SEPARATOR : '=>' ;

REL_OP : ( REL_OP_FULL | REL_OP_SHORT ) ;
REL_OP_FULL : ( 'less-than' | 'less-or-equal' | 'greater-than' | 'greater-or-equal' | 'equal' | 'not-equal' ) ;
REL_OP_SHORT : ( '<' | '<=' | '>' | '>=' | '==' | '!=' ) ;

DEFCONST : 'defconst' ;

STRING : '"' ~["]* '"' ;
SHORT : '-'? [0-9]+ ; // This needs to go at the bottom to make it lowere precedence than FACT_ARG... but that probably breaks defconst... FIXME
SYMBOL : [a-z0-9\-]+ ; // This is likely too restrictive, but covers all of the values actually defined in the TC guide
12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
antlr4-python3-runtime = "*"

[requires]
python_version = "3.6"
28 changes: 28 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions testinput
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(defrule
(player-in-game 1)
(player-human 1)
(goal 1 1)
=>
(chat-to-player 1 "3 Train villagers, build 2 houses, and send 6 to sheep [7 pop](6 sheep)")
(set-goal 1 2)
)

0 comments on commit 1c222f5

Please sign in to comment.