mirror of
https://github.com/mudabbir-ahmad/UNI-PROG3-CW2-DYOPL.git
synced 2026-10-07 20:00:20 +00:00
Refactor RobotLangVisitorUser to improve student ID extraction and clean up code
This commit is contained in:
1 parent
ad6935723e
commit
99e8b68078
3 files changed
+2
-9
No files matched your search
@@ -18,6 +18,6 @@ logStat: 'LOG' STRING ';' ;
|
|||||||
repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ;
|
repeatStat: 'REPEAT' NUMBER '{' stat+ '}' ;
|
||||||
|
|
||||||
ID_PREFIX: '// STUDENT ID:' ;
|
ID_PREFIX: '// STUDENT ID:' ;
|
||||||
NUMBER: [1-9]+ ;
|
NUMBER: [0-9]+ ;
|
||||||
STRING: '"' .*? '"' ;
|
STRING: '"' .*? '"' ;
|
||||||
WS: [ \t\r\n]+ -> skip ;
|
WS: [ \t\r\n]+ -> skip ;
|
||||||
@@ -5,9 +5,7 @@ class RobotLangVisitorUser(RobotLangVisitor):
|
|||||||
self.student_id = "Unknown"
|
self.student_id = "Unknown"
|
||||||
|
|
||||||
def visitHeader(self, ctx):
|
def visitHeader(self, ctx):
|
||||||
# Extract the number directly from the script
|
|
||||||
self.student_id = ctx.NUMBER().getText()
|
self.student_id = ctx.NUMBER().getText()
|
||||||
# Continue walking the rest of the tree
|
|
||||||
return self.visitChildren(ctx)
|
return self.visitChildren(ctx)
|
||||||
|
|
||||||
def visitMoveStat(self, ctx):
|
def visitMoveStat(self, ctx):
|
||||||
@@ -29,14 +27,13 @@ class RobotLangVisitorUser(RobotLangVisitor):
|
|||||||
return self.visitChildren(ctx)
|
return self.visitChildren(ctx)
|
||||||
|
|
||||||
def visitLogStat(self, ctx):
|
def visitLogStat(self, ctx):
|
||||||
val = ctx.STRING().getText()
|
val = ctx.STRING().getText()[1:-1]
|
||||||
print(f"{self.student_id} is logging: {val}.")
|
print(f"{self.student_id} is logging: {val}.")
|
||||||
return self.visitChildren(ctx)
|
return self.visitChildren(ctx)
|
||||||
|
|
||||||
def visitRepeatStat(self, ctx):
|
def visitRepeatStat(self, ctx):
|
||||||
times = int(ctx.NUMBER().getText())
|
times = int(ctx.NUMBER().getText())
|
||||||
for _ in range(times):
|
for _ in range(times):
|
||||||
# Visit all statements inside the repeat block
|
|
||||||
for stat_ctx in ctx.stat():
|
for stat_ctx in ctx.stat():
|
||||||
self.visit(stat_ctx)
|
self.visit(stat_ctx)
|
||||||
return None
|
return None
|
||||||
@@ -11,13 +11,9 @@ def main(argv):
|
|||||||
inpname = argv[1]
|
inpname = argv[1]
|
||||||
inpstream = FileStream(inpname)
|
inpstream = FileStream(inpname)
|
||||||
lexer = RobotLangLexer(inpstream)
|
lexer = RobotLangLexer(inpstream)
|
||||||
#Create a stream of tokens
|
|
||||||
tokstream = CommonTokenStream(lexer)
|
tokstream = CommonTokenStream(lexer)
|
||||||
#Parser produces the abstract syntax tree
|
|
||||||
parser = RobotLangParser(tokstream)
|
parser = RobotLangParser(tokstream)
|
||||||
#Start parsing from the top rule
|
|
||||||
tree = parser.prog()
|
tree = parser.prog()
|
||||||
#execute script
|
|
||||||
visitor = RobotLangVisitorUser()
|
visitor = RobotLangVisitorUser()
|
||||||
visitor.visit(tree)
|
visitor.visit(tree)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user