Skip to content

Commit

Permalink
Merge pull request #8 from morphey-tech/master
Browse files Browse the repository at this point in the history
Adding initial support for expressions like: 
i += 3
b *= a
f /= 10
...
  • Loading branch information
pachanga committed Nov 30, 2021
2 parents 4674dc7 + 91b307f commit eacf6b3
Show file tree
Hide file tree
Showing 9 changed files with 1,934 additions and 1,493 deletions.
6 changes: 5 additions & 1 deletion bhl.g
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ exp
| 'eval' block #ExpEval
| newExp #ExpNew
| exp ternaryIfExp #ExpTernaryIf

;

ternaryIfExp
Expand Down Expand Up @@ -122,6 +121,7 @@ decrementOperator
statement
: varDeclare #VarDecl
| varsDeclareOrCallExps assignExp #DeclAssign
| NAME operatorPostOpAssign exp #VarPostOpAssign
| callExp #SymbCall
| callPostOperators #PostOperatorCall
| mainIf elseIf* else? #If
Expand Down Expand Up @@ -309,6 +309,10 @@ operatorBitAnd
: '&'
;

operatorPostOpAssign
: '+=' | '-=' | '*=' | '/='
;

operatorComparison
: '<' | '>' | '<=' | '>=' | '!=' | '=='
;
Expand Down
39 changes: 37 additions & 2 deletions src/frontend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,34 @@ public override object VisitExpParen(bhlParser.ExpParenContext ctx)
return null;
}

public override object VisitVarPostOpAssign(bhlParser.VarPostOpAssignContext ctx)
{
var lhs = ctx.NAME().GetText();
var vlhs = curr_scope.resolve(lhs) as VariableSymbol;

if(vlhs == null)
FireError(Location(ctx.NAME()) + " : symbol not resolved");

if(!SymbolTable.IsRelopCompatibleType(vlhs.type.type))
throw new UserError(Location(ctx.NAME()) + " : incompatible variable type");

var op = $"{ctx.operatorPostOpAssign().GetText()[0]}";
var op_type = GetBinaryOpType(op);
AST bin_op_ast = AST_Util.New_BinaryOpExp(op_type);

PushAST(bin_op_ast);
bin_op_ast.AddChild(AST_Util.New_Call(EnumCall.VAR, 0, lhs));
Visit(ctx.exp());
PopAST();

SymbolTable.CheckAssign(vlhs.type.type, Wrap(ctx.exp()));

PeekAST().AddChild(bin_op_ast);
PeekAST().AddChild(AST_Util.New_Call(EnumCall.VARW, 0, lhs));

return null;
}

public override object VisitExpAddSub(bhlParser.ExpAddSubContext ctx)
{
var op = ctx.operatorAddSub().GetText();
Expand Down Expand Up @@ -1189,9 +1217,10 @@ public override object VisitExpCompare(bhlParser.ExpCompareContext ctx)
return null;
}

void CommonVisitBinOp(ParserRuleContext ctx, string op, IParseTree lhs, IParseTree rhs)
static EnumBinaryOp GetBinaryOpType(string op)
{
EnumBinaryOp op_type;

if(op == "+")
op_type = EnumBinaryOp.ADD;
else if(op == "-")
Expand All @@ -1215,8 +1244,14 @@ void CommonVisitBinOp(ParserRuleContext ctx, string op, IParseTree lhs, IParseTr
else if(op == "<=")
op_type = EnumBinaryOp.LTE;
else
throw new Exception("Unknown type");
throw new Exception("Unknown type: " + op);

return op_type;
}

void CommonVisitBinOp(ParserRuleContext ctx, string op, IParseTree lhs, IParseTree rhs)
{
EnumBinaryOp op_type = GetBinaryOpType(op);
AST ast = AST_Util.New_BinaryOpExp(op_type);
PushAST(ast);
Visit(lhs);
Expand Down
26 changes: 26 additions & 0 deletions src/g/bhlBaseListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,20 @@ public virtual void EnterDeclAssign([NotNull] bhlParser.DeclAssignContext contex
/// <param name="context">The parse tree.</param>
public virtual void ExitDeclAssign([NotNull] bhlParser.DeclAssignContext context) { }
/// <summary>
/// Enter a parse tree produced by the <c>VarPostOpAssign</c>
/// labeled alternative in <see cref="bhlParser.statement"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void EnterVarPostOpAssign([NotNull] bhlParser.VarPostOpAssignContext context) { }
/// <summary>
/// Exit a parse tree produced by the <c>VarPostOpAssign</c>
/// labeled alternative in <see cref="bhlParser.statement"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void ExitVarPostOpAssign([NotNull] bhlParser.VarPostOpAssignContext context) { }
/// <summary>
/// Enter a parse tree produced by the <c>SymbCall</c>
/// labeled alternative in <see cref="bhlParser.statement"/>.
/// <para>The default implementation does nothing.</para>
Expand Down Expand Up @@ -1451,6 +1465,18 @@ public virtual void EnterOperatorBitAnd([NotNull] bhlParser.OperatorBitAndContex
/// <param name="context">The parse tree.</param>
public virtual void ExitOperatorBitAnd([NotNull] bhlParser.OperatorBitAndContext context) { }
/// <summary>
/// Enter a parse tree produced by <see cref="bhlParser.operatorPostOpAssign"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void EnterOperatorPostOpAssign([NotNull] bhlParser.OperatorPostOpAssignContext context) { }
/// <summary>
/// Exit a parse tree produced by <see cref="bhlParser.operatorPostOpAssign"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
/// <param name="context">The parse tree.</param>
public virtual void ExitOperatorPostOpAssign([NotNull] bhlParser.OperatorPostOpAssignContext context) { }
/// <summary>
/// Enter a parse tree produced by <see cref="bhlParser.operatorComparison"/>.
/// <para>The default implementation does nothing.</para>
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/g/bhlBaseVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,17 @@ public partial class bhlBaseVisitor<Result> : AbstractParseTreeVisitor<Result>,
/// <return>The visitor result.</return>
public virtual Result VisitDeclAssign([NotNull] bhlParser.DeclAssignContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by the <c>VarPostOpAssign</c>
/// labeled alternative in <see cref="bhlParser.statement"/>.
/// <para>
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
/// on <paramref name="context"/>.
/// </para>
/// </summary>
/// <param name="context">The parse tree.</param>
/// <return>The visitor result.</return>
public virtual Result VisitVarPostOpAssign([NotNull] bhlParser.VarPostOpAssignContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by the <c>SymbCall</c>
/// labeled alternative in <see cref="bhlParser.statement"/>.
/// <para>
Expand Down Expand Up @@ -1182,6 +1193,16 @@ public partial class bhlBaseVisitor<Result> : AbstractParseTreeVisitor<Result>,
/// <return>The visitor result.</return>
public virtual Result VisitOperatorBitAnd([NotNull] bhlParser.OperatorBitAndContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by <see cref="bhlParser.operatorPostOpAssign"/>.
/// <para>
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
/// on <paramref name="context"/>.
/// </para>
/// </summary>
/// <param name="context">The parse tree.</param>
/// <return>The visitor result.</return>
public virtual Result VisitOperatorPostOpAssign([NotNull] bhlParser.OperatorPostOpAssignContext context) { return VisitChildren(context); }
/// <summary>
/// Visit a parse tree produced by <see cref="bhlParser.operatorComparison"/>.
/// <para>
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
Expand Down
Loading

0 comments on commit eacf6b3

Please sign in to comment.