public List <Instruction> VisitFunction(Scope context, AstExpression.Func func)
        {
            if (func.TokenName.Value == "=" &&
                func.Arguments.Length == 2 &&
                func.Arguments[0].Type == func.Arguments[1].Type)
            {
                if (func.Arguments[0].Type != DataTypes.Int)
                {
                    throw new NotImplementedException();
                }

                var variableExpression = func.Arguments[0] as AstExpression.Var;
                if (variableExpression == null)
                {
                    throw new Exception("Left hand side of operator '=' must be a variable");
                }

                var list = func.Arguments[1].Accept(context, this);
                list.Add(DupInt);
                list.Add(WriteLocal, context.GetHeapLayoutUntil(variableExpression.Variable));
                return(list);
            }
            else if (func.Function.IsBuildIn)
            {
                var list = func.Arguments.SelectMany(arg => arg.Accept(context, this)).ToList();
                list.AddRange(func.Function.Body);
                return(list);
            }
            else
            {
                var list = func.Arguments.SelectMany(arg => arg.Accept(context, this)).ToList();
                list.Add(Invoke, new int[] { func.Function.Id });
                return(list);
            }
        }
        public AstExpression VisitFunction(Scope context, AstExpression.Func func)
        {
            for (int i = 0; i < func.Arguments.Length; i++)
            {
                func.Arguments[i] = func.Arguments[i].Accept(context, this);
            }

            var argTypes = func.Arguments.Select(arg => arg.Type).ToArray();

            func.Function = context.FindFunction(func.TokenName.Value, argTypes)
                            ?? throw new CompilerException(
                                      func.TokenName,
                                      $"cannot find function '{func.TokenName.Value}({string.Join<DataType>(",", argTypes)}).");
            return(func);
        }
Example #3
0
        private AstExpression ParseExpression(int priority)
        {
            this.NextToken();
            AstExpression left = this.ParseSimpleExpression();

            while (this.Tokenizer.CanPeek() &&
                   operators.ContainsKey(this.Tokenizer.Peek().Type) &&
                   operators[this.Tokenizer.Peek().Type] >= priority)
            {
                var op    = this.NextToken();
                var right = this.ParseExpression(operators[op.Type] + 1);
                left = new AstExpression.Func(op, left, right);
            }

            return(left);
        }
 public Tree VisitFunction(object c, AstExpression.Func func)
 => new Tree("f:" + func.TokenName.Value, func.Arguments.Select(p => p.Accept(c, this)).ToArray());