mirror of
https://github.com/mudabbir-ahmad/UNI-PROG3-CW2-DYOPL.git
synced 2026-10-07 20:00:20 +00:00
Add initial implementation of RobotLang interpreter and visitor pattern
This commit is contained in:
1 parent
4929d6b01e
commit
ea6d32b726
7 files changed
+114
-1
No files matched your search
@@ -0,0 +1,17 @@
|
||||
// STUDENT ID: 2320390
|
||||
WAIT 1;
|
||||
RELEASE;
|
||||
LOG "Log entry 592";
|
||||
REPEAT 3 {
|
||||
GRAB;
|
||||
GRAB;
|
||||
}
|
||||
REPEAT 3 {
|
||||
GRAB;
|
||||
LOG "Finished 962";
|
||||
}
|
||||
REPEAT 4 {
|
||||
WAIT 2;
|
||||
MOVE 7;
|
||||
MOVE 14;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
grammar RobotLang;
|
||||
|
||||
prog: stat+ ;
|
||||
|
||||
stat: moveStat
|
||||
| grabStat
|
||||
| releaseStat
|
||||
| waitStat
|
||||
| logStat
|
||||
| repeatStat
|
||||
;
|
||||
|
||||
moveStat: 'MOVE' NUMBER ';' ;
|
||||
grabStat: 'GRAB' ';' ;
|
||||
releaseStat: 'RELEASE' ';' ;
|
||||
waitStat: 'WAIT' NUMBER ';' ;
|
||||
logStat: 'LOG' STRING ';' ;
|
||||
repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ;
|
||||
|
||||
NUMBER: [0-9]+ ;
|
||||
STRING: '"' .*? '"' ;
|
||||
WS: [ \t\r\n]+ -> skip ;
|
||||
@@ -0,0 +1,43 @@
|
||||
from antlr4 import *
|
||||
if "." in __name__:
|
||||
from .RobotLangParser import RobotLangParser
|
||||
else:
|
||||
from RobotLangParser import RobotLangParser
|
||||
|
||||
class RobotLangVisitorUser(RobotLangVisitor):
|
||||
def __init__(self, student_id="2320390"):
|
||||
self.student_id = student_id
|
||||
|
||||
def visitMoveStat(self, ctx):
|
||||
val = ctx.NUMBER().getText()
|
||||
print(f"{self.student_id} is moving by {val}.")
|
||||
return self.visitChildren(ctx)
|
||||
|
||||
def visitGrabStat(self, ctx):
|
||||
print(f"{self.student_id} is grabbing.")
|
||||
return self.visitChildren(ctx)
|
||||
|
||||
def visitReleaseStat(self, ctx):
|
||||
print(f"{self.student_id} is releasing.")
|
||||
return self.visitChildren(ctx)
|
||||
|
||||
def visitWaitStat(self, ctx):
|
||||
val = ctx.NUMBER().getText()
|
||||
print(f"{self.student_id} is waiting for {val}.")
|
||||
return self.visitChildren(ctx)
|
||||
|
||||
def visitLogStat(self, ctx):
|
||||
val = ctx.STRING().getText()
|
||||
print(f"{self.student_id} is logging: {val}.")
|
||||
return self.visitChildren(ctx)
|
||||
|
||||
def visitRepeatStat(self, ctx):
|
||||
times = int(ctx.NUMBER().getText())
|
||||
for _ in range(times):
|
||||
# Visit all statements inside the repeat block
|
||||
for stat_ctx in ctx.stat():
|
||||
self.visit(stat_ctx)
|
||||
return None
|
||||
|
||||
|
||||
del RobotLangParser
|
||||
@@ -0,0 +1,31 @@
|
||||
import sys
|
||||
from antlr4 import *
|
||||
from RobotLangLexer import RobotLangLexer
|
||||
from RobotLangParser import RobotLangParser
|
||||
from RobotLangVisitorUser import RobotLangVisitorUser
|
||||
|
||||
def main(argv):
|
||||
# Determine if reading from a file or standard input
|
||||
if len(argv) == 1:
|
||||
inpstream = StdinStream()
|
||||
else:
|
||||
inpname = argv[9]
|
||||
inpstream = FileStream(inpname)
|
||||
# 1. Lexer analyses the characters
|
||||
lexer = RobotLangLexer(inpstream)
|
||||
# 2. Create a stream of tokens
|
||||
tokstream = CommonTokenStream(lexer)
|
||||
# 3. Parser produces the abstract syntax tree
|
||||
parser = RobotLangParser(tokstream)
|
||||
# 4. Start parsing from the top rule ('prog' in your grammar)
|
||||
tree = parser.prog()
|
||||
|
||||
# Printing the parse tree using Marina's PrettyPrint and toStringTree methods (Not-needed for the assignment)
|
||||
# print(TreesUser.toStringTree(tree, None, parser))
|
||||
# print(TreesUser.PrettyPrint(tree, None, parser))
|
||||
|
||||
visitor = RobotLangVisitorUser("2320390")
|
||||
visitor.visit(tree)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
BIN
Binary file not shown.
Reference in new issue
Block a user