Skip to content

Commit

Permalink
Merge pull request #11 from z80rotom/rework-lexer
Browse files Browse the repository at this point in the history
Fixed bug causing duplicate arguments
  • Loading branch information
z80rotom authored Jan 16, 2022
2 parents 7af6a22 + c8c0e6e commit 194be9d
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 10,444 deletions.
62 changes: 16 additions & 46 deletions ev.g4

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions src/evAssembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ def encode_float(var):
return data

class evAssembler(evListener):
def __init__(self):
def __init__(self, fileName):
self.fileName = fileName
self.currentLabel = None
self.scripts = {}
self.strTbl = []
self.currCmdIdx = -1
self.writer = EndianBinaryWriter()

def enterLbl(self, ctx: evParser.LblContext):
lbl = ctx.getChild(0)
lbl = ctx.getChild(0).getChild(0)
# print("enterLbl: {}".format(lbl))
# If someone can get the grammar working a bit better
# then this replace can go away, but I can't get the :
# in the right rule not to do this without making labels
Expand All @@ -58,12 +60,11 @@ def enterLbl(self, ctx: evParser.LblContext):
self.scripts[self.currentLabel] = []
self.currCmdIdx = -1

def enterEvCmd(self, ctx:evParser.EvCmdContext):
name = str(ctx.getChild(0))
# Enter a parse tree produced by evParser#instruction.
def enterInstruction(self, ctx:evParser.InstructionContext):
name = str(ctx.getChild(0).getChild(0))
if not hasattr(EvCmdType, name):
# NOTE: should probably be an error
print("Invalid EvCmd: {}".format(name))
return
raise RuntimeError("Invalid EvCmd: {} at {}:{}:{}".format(name, self.fileName, ctx.start.line, ctx.start.column))
evCmdType = getattr(EvCmdType, name)
args = []
evCmd = EvCmd(evCmdType, args, ctx.start.line, ctx.start.column)
Expand All @@ -81,7 +82,7 @@ def enterNumber(self, ctx: evParser.NumberContext):
)

def enterWork(self, ctx: evParser.WorkContext):
argVal = str(ctx.getChild(0))[1:]
argVal = str(ctx.getChild(1))

if argVal.isdigit():
argVal = int(argVal)
Expand All @@ -90,17 +91,17 @@ def enterWork(self, ctx: evParser.WorkContext):
if hasattr(EvWork, argVal):
argVal = getattr(EvWork, argVal)
else:
raise RuntimeError("Unknown work: {}. Cannot convert to number {}:{}".format(argVal, ctx.start.line, ctx.start.column))
raise RuntimeError("Unknown work: @{}. Cannot convert to number {}:{}:{}".format(argVal, self.fileName, ctx.start.line, ctx.start.column))

self.scripts[self.currentLabel][self.currCmdIdx].args.append(
EvArg(EvArgType.Work, argVal, ctx.start.line, ctx.start.column)
)

if argVal > MAX_WORK:
print("[Warning] line {}:{} Invalid work: @{}".format(ctx.start.line, ctx.start.column, argVal))
print("[Warning] line {}:{}:{} Invalid work: @{}".format(self.fileName, ctx.start.line, ctx.start.column, argVal))

def enterFlag(self, ctx: evParser.FlagContext):
argVal = str(ctx.getChild(0))[1:]
argVal = str(ctx.getChild(1))

if argVal.isdigit():
argVal = int(argVal)
Expand All @@ -109,17 +110,17 @@ def enterFlag(self, ctx: evParser.FlagContext):
if hasattr(EvFlag, argVal):
argVal = getattr(EvFlag, argVal)
else:
raise RuntimeError("Unknown Flag: {}. Cannot convert to number {}:{}".format(argVal, ctx.start.line, ctx.start.column))
raise RuntimeError("Unknown Flag: #{}. Cannot convert to number {}:{}:{}".format(argVal, self.fileName, ctx.start.line, ctx.start.column))

self.scripts[self.currentLabel][self.currCmdIdx].args.append(
EvArg(EvArgType.Flag, argVal, ctx.start.line, ctx.start.column)
)

if argVal > MAX_FLAG:
print("[Warning] line {}:{} Invalid Flag: #{}".format(ctx.start.line, ctx.start.column, argVal))
print("[Warning] line {}:{}:{} Invalid Flag: #{}".format(self.fileName, ctx.start.line, ctx.start.column, argVal))

def enterSysFlag(self, ctx: evParser.SysFlagContext):
argVal = str(ctx.getChild(0))[1:]
argVal = str(ctx.getChild(1))

if argVal.isdigit():
argVal = int(argVal)
Expand All @@ -128,7 +129,7 @@ def enterSysFlag(self, ctx: evParser.SysFlagContext):
if hasattr(EvSysFlag, argVal):
argVal = getattr(EvSysFlag, argVal)
else:
raise RuntimeError("Unknown SysFlag: {}. Cannot convert to number {}:{}".format(argVal, ctx.start.line, ctx.start.column))
raise RuntimeError("Unknown SysFlag: ${}. Cannot convert to number {}:{}".format(argVal, ctx.start.line, ctx.start.column))

self.scripts[self.currentLabel][self.currCmdIdx].args.append(
EvArg(EvArgType.SysFlag, argVal, ctx.start.line, ctx.start.column)
Expand Down
Loading

0 comments on commit 194be9d

Please sign in to comment.