mirror of
https://github.com/mudabbir-ahmad/UNI-PROG3-CW2-DYOPL.git
synced 2026-10-08 04: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,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
|
||||
Reference in new issue
Block a user