Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] generated runtime errors #36

Merged
merged 4 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mccode_antlr/common/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def _str_repr_(self, lstr, rstr):
return f'{lstr} || {rstr}' if 'C' == self.style else f'{lstr} or {rstr}'
if '__and__' == self.op:
return f'{lstr} && {rstr}' if 'C' == self.style else f'{lstr} and {rstr}'
if any(x in self.op for x in '+-'):
if any(x == self.op for x in ('+', '-', '%', '<<', '>>')):
return f'({lstr} {self.op} {rstr})'
if self.op == '//' and 'C' == self.style:
# Verify that the operands are integers before reducing to a single slash?
Expand Down
16 changes: 16 additions & 0 deletions mccode_antlr/comp/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ def visitExpressionBinaryMD(self, ctx: Parser.ExpressionBinaryMDContext):
left, right = self.visit(ctx.left), self.visit(ctx.right)
return left * right if ctx.Div() is None else left / right


def visitExpressionBinaryMod(self, ctx: Parser.ExpressionBinaryModContext):
from ..common import BinaryOp
left, right = self.visit(ctx.left), self.visit(ctx.right)
return Expr(BinaryOp('%', left, right))

def visitExpressionBinaryLeftShift(self, ctx: Parser.ExpressionBinaryLeftShiftContext):
from ..common import BinaryOp
left, right = self.visit(ctx.left), self.visit(ctx.right)
return Expr(BinaryOp('<<', left, right))

def visitExpressionBinaryRightShift(self, ctx: Parser.ExpressionBinaryRightShiftContext):
from ..common import BinaryOp
left, right = self.visit(ctx.left), self.visit(ctx.right)
return Expr(BinaryOp('>>', left, right))

def visitInitializerlist(self, ctx: Parser.InitializerlistContext):
from ..common import Value, ObjectType, ShapeType
values = [self.visit(x).expr[0].value for x in ctx.values]
Expand Down
14 changes: 14 additions & 0 deletions mccode_antlr/compiler/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ def __init__(self, mpi: bool = False, acc: bool = False, count: int = 1, nexus:
if nexus:
self.type |= CBinaryTarget.Type.nexus

def update(self, d: dict):
self.count = d.get('count', self.count)
d_mpi = d.get('mpi', self.type & CBinaryTarget.Type.mpi)
d_acc = d.get('acc', self.type & CBinaryTarget.Type.acc)
if d_mpi and d_acc:
self.type = CBinaryTarget.Type.acc | CBinaryTarget.Type.mpi
elif d_mpi:
self.type = CBinaryTarget.Type.mpi
elif d_acc:
self.type = CBinaryTarget.Type.acc
else:
self.type = CBinaryTarget.Type.single
if d.get('nexus', self.type & CBinaryTarget.Type.nexus):
self.type |= CBinaryTarget.Type.nexus

@property
def compiler(self) -> str:
Expand Down
5 changes: 4 additions & 1 deletion mccode_antlr/grammar/McCommon.g4
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ expr
| <assoc=right> base=expr '^' exponent=expr #ExpressionExponentiation
| left=expr ('*' | '/') right=expr #ExpressionBinaryMD
| left=expr ('+' | '-') right=expr #ExpressionBinaryPM
| left=expr '%' right=expr #ExpressionBinaryMod
| left=expr '>>' right=expr #ExpressionBinaryRightShift
| left=expr '<<' right=expr #ExpressionBinaryLeftShift
| Identifier #ExpressionIdentifier
| left=expr '==' right=expr #ExpressionBinaryEqual
| left=expr '<=' right=expr #ExpressionBinaryLessEqual
Expand Down Expand Up @@ -81,7 +84,7 @@ Category: 'CATEGORY' | 'Category' | 'category';
Component : 'COMPONENT' | 'Component' | 'component';
UserVars: 'USERVARS' | 'UserVars' | 'uservars';
Define: 'DEFINE' | 'Define' | 'define';
Declare: 'DECLARE';
Declare: 'DECLARE' | 'Declare' | 'declare';
Definition: 'DEFINITION' | 'Definition' | 'definition';
End: 'END' | 'End' | 'end';
McDisplay: 'MCDISPLAY' | 'DISPLAY' | 'McDisplay' | 'mcdisplay' | 'Display' | 'display';
Expand Down
2,345 changes: 1,179 additions & 1,166 deletions mccode_antlr/grammar/McCompLexer.py

Large diffs are not rendered by default.

173 changes: 100 additions & 73 deletions mccode_antlr/grammar/McCompListener.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated from /home/g/Code/mccode_antlr-antlr/mccode_antlr/grammar/McComp.g4 by ANTLR 4.13.1
# Generated from /home/gst/PycharmProjects/mccode4/mccode_antlr/grammar/McComp.g4 by ANTLR 4.13.1
from antlr4 import *
if "." in __name__:
from .McCompParser import McCompParser
Expand Down Expand Up @@ -350,6 +350,15 @@ def exitAssignment(self, ctx:McCompParser.AssignmentContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryMod.
def enterExpressionBinaryMod(self, ctx:McCompParser.ExpressionBinaryModContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionBinaryMod.
def exitExpressionBinaryMod(self, ctx:McCompParser.ExpressionBinaryModContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryLess.
def enterExpressionBinaryLess(self, ctx:McCompParser.ExpressionBinaryLessContext):
pass
Expand All @@ -368,15 +377,6 @@ def exitExpressionBinaryGreater(self, ctx:McCompParser.ExpressionBinaryGreaterCo
pass


# Enter a parse tree produced by McCompParser#ExpressionGrouping.
def enterExpressionGrouping(self, ctx:McCompParser.ExpressionGroupingContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionGrouping.
def exitExpressionGrouping(self, ctx:McCompParser.ExpressionGroupingContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryLessEqual.
def enterExpressionBinaryLessEqual(self, ctx:McCompParser.ExpressionBinaryLessEqualContext):
pass
Expand Down Expand Up @@ -413,6 +413,87 @@ def exitExpressionInteger(self, ctx:McCompParser.ExpressionIntegerContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryRightShift.
def enterExpressionBinaryRightShift(self, ctx:McCompParser.ExpressionBinaryRightShiftContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionBinaryRightShift.
def exitExpressionBinaryRightShift(self, ctx:McCompParser.ExpressionBinaryRightShiftContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionMyself.
def enterExpressionMyself(self, ctx:McCompParser.ExpressionMyselfContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionMyself.
def exitExpressionMyself(self, ctx:McCompParser.ExpressionMyselfContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionPrevious.
def enterExpressionPrevious(self, ctx:McCompParser.ExpressionPreviousContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionPrevious.
def exitExpressionPrevious(self, ctx:McCompParser.ExpressionPreviousContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionIdentifier.
def enterExpressionIdentifier(self, ctx:McCompParser.ExpressionIdentifierContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionIdentifier.
def exitExpressionIdentifier(self, ctx:McCompParser.ExpressionIdentifierContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionStructAccess.
def enterExpressionStructAccess(self, ctx:McCompParser.ExpressionStructAccessContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionStructAccess.
def exitExpressionStructAccess(self, ctx:McCompParser.ExpressionStructAccessContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionFunctionCall.
def enterExpressionFunctionCall(self, ctx:McCompParser.ExpressionFunctionCallContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionFunctionCall.
def exitExpressionFunctionCall(self, ctx:McCompParser.ExpressionFunctionCallContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryMD.
def enterExpressionBinaryMD(self, ctx:McCompParser.ExpressionBinaryMDContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionBinaryMD.
def exitExpressionBinaryMD(self, ctx:McCompParser.ExpressionBinaryMDContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionString.
def enterExpressionString(self, ctx:McCompParser.ExpressionStringContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionString.
def exitExpressionString(self, ctx:McCompParser.ExpressionStringContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionGrouping.
def enterExpressionGrouping(self, ctx:McCompParser.ExpressionGroupingContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionGrouping.
def exitExpressionGrouping(self, ctx:McCompParser.ExpressionGroupingContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionExponentiation.
def enterExpressionExponentiation(self, ctx:McCompParser.ExpressionExponentiationContext):
pass
Expand All @@ -422,6 +503,15 @@ def exitExpressionExponentiation(self, ctx:McCompParser.ExpressionExponentiation
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryLeftShift.
def enterExpressionBinaryLeftShift(self, ctx:McCompParser.ExpressionBinaryLeftShiftContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionBinaryLeftShift.
def exitExpressionBinaryLeftShift(self, ctx:McCompParser.ExpressionBinaryLeftShiftContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryGreaterEqual.
def enterExpressionBinaryGreaterEqual(self, ctx:McCompParser.ExpressionBinaryGreaterEqualContext):
pass
Expand All @@ -440,15 +530,6 @@ def exitExpressionZero(self, ctx:McCompParser.ExpressionZeroContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionMyself.
def enterExpressionMyself(self, ctx:McCompParser.ExpressionMyselfContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionMyself.
def exitExpressionMyself(self, ctx:McCompParser.ExpressionMyselfContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionUnaryPM.
def enterExpressionUnaryPM(self, ctx:McCompParser.ExpressionUnaryPMContext):
pass
Expand All @@ -458,15 +539,6 @@ def exitExpressionUnaryPM(self, ctx:McCompParser.ExpressionUnaryPMContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionPrevious.
def enterExpressionPrevious(self, ctx:McCompParser.ExpressionPreviousContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionPrevious.
def exitExpressionPrevious(self, ctx:McCompParser.ExpressionPreviousContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionTrinaryLogic.
def enterExpressionTrinaryLogic(self, ctx:McCompParser.ExpressionTrinaryLogicContext):
pass
Expand Down Expand Up @@ -494,15 +566,6 @@ def exitExpressionPointerAccess(self, ctx:McCompParser.ExpressionPointerAccessCo
pass


# Enter a parse tree produced by McCompParser#ExpressionIdentifier.
def enterExpressionIdentifier(self, ctx:McCompParser.ExpressionIdentifierContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionIdentifier.
def exitExpressionIdentifier(self, ctx:McCompParser.ExpressionIdentifierContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryEqual.
def enterExpressionBinaryEqual(self, ctx:McCompParser.ExpressionBinaryEqualContext):
pass
Expand Down Expand Up @@ -530,42 +593,6 @@ def exitExpressionUnaryLogic(self, ctx:McCompParser.ExpressionUnaryLogicContext)
pass


# Enter a parse tree produced by McCompParser#ExpressionStructAccess.
def enterExpressionStructAccess(self, ctx:McCompParser.ExpressionStructAccessContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionStructAccess.
def exitExpressionStructAccess(self, ctx:McCompParser.ExpressionStructAccessContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionFunctionCall.
def enterExpressionFunctionCall(self, ctx:McCompParser.ExpressionFunctionCallContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionFunctionCall.
def exitExpressionFunctionCall(self, ctx:McCompParser.ExpressionFunctionCallContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionBinaryMD.
def enterExpressionBinaryMD(self, ctx:McCompParser.ExpressionBinaryMDContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionBinaryMD.
def exitExpressionBinaryMD(self, ctx:McCompParser.ExpressionBinaryMDContext):
pass


# Enter a parse tree produced by McCompParser#ExpressionString.
def enterExpressionString(self, ctx:McCompParser.ExpressionStringContext):
pass

# Exit a parse tree produced by McCompParser#ExpressionString.
def exitExpressionString(self, ctx:McCompParser.ExpressionStringContext):
pass


# Enter a parse tree produced by McCompParser#shell.
def enterShell(self, ctx:McCompParser.ShellContext):
pass
Expand Down
Loading