Skip to content

Commit

Permalink
Merge pull request #9 from z80rotom/revert-8-rework-lexer
Browse files Browse the repository at this point in the history
Revert "Rework lexer"
  • Loading branch information
z80rotom authored Jan 11, 2022
2 parents 8054474 + 40c88c6 commit 7af6a22
Show file tree
Hide file tree
Showing 6 changed files with 10,446 additions and 480 deletions.
62 changes: 46 additions & 16 deletions ev.g4

Large diffs are not rendered by default.

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

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

def enterLbl(self, ctx: evParser.LblContext):
lbl = ctx.getChild(0).getChild(0)
# print("enterLbl: {}".format(lbl))
lbl = ctx.getChild(0)
# 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 @@ -60,11 +58,12 @@ def enterLbl(self, ctx: evParser.LblContext):
self.scripts[self.currentLabel] = []
self.currCmdIdx = -1

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

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

if argVal.isdigit():
argVal = int(argVal)
Expand All @@ -91,17 +90,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, self.fileName, ctx.start.line, ctx.start.column))
raise RuntimeError("Unknown work: {}. Cannot convert to number {}:{}".format(argVal, 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(self.fileName, ctx.start.line, ctx.start.column, argVal))
print("[Warning] line {}:{} Invalid work: @{}".format(ctx.start.line, ctx.start.column, argVal))

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

if argVal.isdigit():
argVal = int(argVal)
Expand All @@ -110,17 +109,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, self.fileName, ctx.start.line, ctx.start.column))
raise RuntimeError("Unknown Flag: {}. Cannot convert to number {}:{}".format(argVal, 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(self.fileName, ctx.start.line, ctx.start.column, argVal))
print("[Warning] line {}:{} Invalid Flag: #{}".format(ctx.start.line, ctx.start.column, argVal))

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

if argVal.isdigit():
argVal = int(argVal)
Expand All @@ -129,7 +128,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 7af6a22

Please sign in to comment.