public override int VisitAssign(BRAQParser.AssignContext context) { if (context.id_name != null) { context.assignee.Accept(this); var target_token = context.id_name; if (variable_to_declaration.ContainsKey(target_token)) { var assignment_point = variable_to_declaration[target_token]; int var_id = Array.IndexOf(locals, assignment_point); il.Emit(OpCodes.Stloc, var_id); } int n = current_method.arguments.IndexOf( current_method.arguments.Find(x => context.id_name.Text == x.a)); il.Emit(OpCodes.Starg, n); } else { context.assignee.Accept(this); } return(0); }
public override int VisitAssign(BRAQParser.AssignContext context) { context.assignee.Accept(this); if (context.id_name == null) { return(0); } if (TryResolve(context.id_name, out var var_definition_point)) { declaration[context.id_name] = var_definition_point; if (!assigned[var_definition_point]) { assigners[var_definition_point] = context.assignee; assigned[var_definition_point] = true; } } else if (TryResolveAsArg(context.id_name, out var argument_definition)) { token_to_argument[context.id_name] = argument_definition; } else { Console.WriteLine($"unknown variable {context.id_name.Text} [Line {context.id_name.Line}]"); throw new UnknownVariableException(); } return(0); }
public override Type VisitAssign(BRAQParser.AssignContext context) { Type right_type = context.assignee.Accept(this); if (context.id_name != null) { if (var_to_local_def.ContainsKey(context.id_name)) { Type target_type = locals_type[var_to_local_def[context.id_name]]; //check types if (target_type != right_type) { Console.WriteLine( $"type mismatch: assigned {right_type} to variable {context.id_name.Text} of type {target_type} [Line {context.id_name.Line}]"); throw new TypeMismatchError(); } return(type_dict[context] = typeof(void)); } try { Type target_type = current_method.arguments.First(x => x.a == context.id_name.Text).b; if (target_type != right_type) { Console.WriteLine( $"type mismatch: assigned {right_type} to variable {context.id_name.Text} of type {target_type} [Line {context.id_name.Line}]"); throw new TypeMismatchError(); } } catch (InvalidOperationException) { Console.WriteLine($"unknown variable {context.id_name.Text} [Line {context.id_name.Line}]"); throw new UnknownVariableException(); } return(type_dict[context] = typeof(void)); } return(type_dict[context] = right_type); }
/// <summary> /// Visit a parse tree produced by <see cref="BRAQParser.assign"/>. /// <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 VisitAssign([NotNull] BRAQParser.AssignContext context) { return(VisitChildren(context)); }
/// <summary> /// Exit a parse tree produced by <see cref="BRAQParser.assign"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitAssign([NotNull] BRAQParser.AssignContext context) { }