public static NeuOperation CallVTable(
        this NeuInterpreter interpreter,
        String name,
        IEnumerable <Node> rawArgs,
        IList <NeuArgument> evalArguments)
    {
        ///

        var ops = interpreter.GetOperations(name, null, null, null);

        ///

        var callee = ops.SingleOrDefault() as NeuOperation;

        if (callee == null)
        {
            throw new Exception();
        }

        ///

        var calleeeParamClause = callee.GetParamClause();

        if (calleeeParamClause == null)
        {
            throw new Exception();
        }

        ///

        var calleeParamList = calleeeParamClause.GetFuncParamList();

        if (calleeParamList == null)
        {
            throw new Exception();
        }

        ///

        switch (true)
        {
        case var _ when rawArgs.Count() == 0 && calleeParamList.Count() == 0:

            break;

        ///

        default:

            throw new Exception();
        }

        ///

        return(interpreter.Execute(callee, evalArguments));
    }
Beispiel #2
0
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuExpression expr)
    {
        switch (expr)
        {
        case NeuLiteralExpression litExpr:

            return(interpreter.Execute(litExpr));

        ///

        case NeuIdentifierExpression idExpr:

            return(interpreter.Execute(idExpr));

        ///

        case NeuCallExpression callExpr:

            return(interpreter.Execute(callExpr));



        ///

        case NeuUnaryExpression unaryExpr when !unaryExpr.IsFixExpression():

            return(interpreter.Execute(unaryExpr));

        ///

        case NeuPrefixExpression prefixExpr:

            return(interpreter.Execute(prefixExpr));

        ///

        case NeuInfixExpression infixExpr:

            return(interpreter.Execute(infixExpr));

        ///

        case NeuPostfixExpression postfixExpr:

            return(interpreter.Execute(postfixExpr));


        ///

        default:

            throw new Exception();
        }
    }
Beispiel #3
0
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuExpressionStatement expressionStatement)
    {
        var expression = expressionStatement.GetExpression();

        if (expression == null)
        {
            throw new Exception();
        }

        return(interpreter.Execute(expression));
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuDeclaration declaration)
    {
        switch (declaration)
        {
        case NeuFuncDecl funcDecl:

            return(interpreter.Execute(funcDecl));

        ///

        case NeuVarDecl varDecl:

            return(interpreter.Execute(varDecl));

        ///

        default:

            throw new Exception();
        }
    }
Beispiel #5
0
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuStatement statement)
    {
        switch (statement)
        {
        case NeuExpressionStatement exprStmt:

            return(interpreter.Execute(exprStmt));

        ///

        case NeuReturnStatement returnStatement:

            return(interpreter.Execute(returnStatement));

        ///

        default:

            throw new Exception();
        }
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuNumberLiteralExpression numberLiteralExpr)
    {
        var numberLit = numberLiteralExpr.GetNumberLiteral();

        if (numberLit == null)
        {
            throw new Exception();
        }

        ///

        return(interpreter.Execute(numberLit));
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuIdentifierExpression idExpr)
    {
        var id = idExpr.GetIdentifier();

        if (id == null)
        {
            throw new Exception();
        }

        ///

        return(interpreter.Execute(id));
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuBoolLiteralExpression boolLiteralExpr)
    {
        var boolKeyword = boolLiteralExpr.GetBoolKeyword();

        if (boolKeyword == null)
        {
            throw new Exception();
        }

        ///

        return(interpreter.Execute(boolKeyword));
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuReturnStatement retStmt)
    {
        var argument = retStmt.GetArgument();

        if (argument == null)
        {
            return(new NeuReturnResult(NeuOperation.Void));
        }

        ///

        var result = interpreter.Execute(argument);

        ///

        return(new NeuReturnResult(result));
    }
Beispiel #10
0
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuSizeOfExpression sizeOfExpr)
    {
        var typeId = sizeOfExpr.GetTypeIdentifier();

        ///

        switch (typeId)
        {
        case NeuSimpleTypeId simpleTypeId:

            return(interpreter.Execute(simpleTypeId));

        ///

        default:

            throw new Exception();
        }
    }
    public static NeuOperation Call(
        this NeuInterpreter interpreter,
        String name,
        IEnumerable <Node> rawArgs)
    {
        var evalArguments = new List <NeuArgument>();

        ///

        foreach (var rawArg in rawArgs)
        {
            var argValue = interpreter.Execute(rawArg);

            ///

            var arg = new NeuArgument(null, argValue);

            ///

            evalArguments.Add(arg);
        }

        ///

        switch (true)
        {
        case var _ when IsNeuPrimitive(name):

            return(interpreter.CallPrimitive(name, rawArgs, evalArguments));

        ///

        default:

            return(interpreter.CallVTable(name, rawArgs, evalArguments));
        }
    }
Beispiel #12
0
    public static NeuOperation PrefixIncrement(
        this NeuInterpreter interpreter,
        NeuExpression operand)
    {
        var id = operand.GetIdentifier();

        if (id == null)
        {
            throw new Exception();
        }

        ///

        var name = id.Source;

        ///

        var operandResult = interpreter.Execute(operand);

        ///

        switch (true)
        {
        case var _
            when
            operandResult is NeuInteger intResult:

            return(interpreter.PrefixIncrement(name, intResult));

        ///

        default:

            throw new Exception();
        }
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuPrefixExpression prefixExpr)
    {
        var op = prefixExpr.GetOperator();

        if (op == null)
        {
            throw new Exception();
        }

        ///

        var operand = prefixExpr.GetOperand() as NeuExpression;

        if (operand == null)
        {
            throw new Exception();
        }

        ///

        return(interpreter.Execute(op, operand));
    }
Beispiel #14
0
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        Node node)
    {
        switch (node)
        {
        /// AST Nodes

        case NeuDeclaration decl:

            return(interpreter.Execute(decl));

        ///

        case NeuExpression expr:

            return(interpreter.Execute(expr));

        ///

        case NeuStatement stmt:

            return(interpreter.Execute(stmt));

        ///

        case NeuNode neuNode:

            return(interpreter.Execute(neuNode));

        /// Tokens

        case NeuLiteral literal:

            return(interpreter.Execute(literal));

        ///

        case NeuIdentifier id:

            return(interpreter.Execute(id));

        ///

        case NeuKeyword k when k.KeywordType == NeuKeywordType.True:

            return(new NeuBool(true));

        ///

        case NeuKeyword k when k.KeywordType == NeuKeywordType.False:

            return(new NeuBool(false));

        ///

        case var n:

            WriteLine($"NeuInterpreter no op: {n.Dump()}");

            return(NeuValue.Void);
        }
    }
Beispiel #15
0
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuNode node,
        params object[] arguments)
    {
        interpreter.Enter(node);

        ///



        ///

        // TODO: Add hoists

        ///

        var enterPos = interpreter.Stack.Count();

        ///

        var lastValue = NeuOperation.Void;

        ///

        var done = false;

        ///

        for (var i = 0; i < node.Children.Count() && !done; i++)
        {
            var child = node.Children.ElementAt(i);

            ///

            if (child is NeuPunc)
            {
                continue;
            }

            ///

            var childResult = interpreter.Execute(child);

            ///

            switch (childResult)
            {
            case NeuReturnResult returnResult:

                lastValue = returnResult;

                done = true;

                break;

            ///

            case NeuValue value:

                lastValue = value;

                break;

            ///

            default:

                break;
            }
        }

        ///

        interpreter.Unwind(enterPos, node);

        ///

        interpreter.Exit(node);

        ///

        return(lastValue);
    }
    ///

    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        bool inGlobalScope,
        NeuKeyword kindKeyword,
        NeuIdentifierPattern idPattern,
        NeuInitClause initClause)
    {
        var id = idPattern.GetIdentifier();

        if (id == null)
        {
            throw new Exception();
        }

        ///

        var name = id.Source;

        if (IsNullOrWhiteSpace(name))
        {
            throw new Exception();
        }

        ///

        var initializer = initClause.GetInitializer();

        if (initializer == null)
        {
            throw new Exception();
        }

        ///

        var initValue = interpreter.Execute(initializer);

        ///

        switch (true)
        {
        case var _
            when
            inGlobalScope &&
            kindKeyword.KeywordType == NeuKeywordType.Let:

            var globalScopedLet = interpreter.CreateVar(name, null, null, null, initValue);

            if (globalScopedLet == null)
            {
                throw new Exception();
            }

            return(globalScopedLet);

        ///

        case var _
            when
            inGlobalScope &&
            kindKeyword.KeywordType == NeuKeywordType.Var:

            var globalScopedVar = interpreter.CreateVar(name, null, null, null, initValue);

            if (globalScopedVar == null)
            {
                throw new Exception();
            }

            return(globalScopedVar);

        ///

        case var _
            when
            kindKeyword.KeywordType == NeuKeywordType.Let:

            var hoistedLet = interpreter.HoistVar(kindKeyword.KeywordType, name, initValue);

            if (hoistedLet == null)
            {
                throw new Exception();
            }

            return(hoistedLet);

        ///

        case var _
            when
            kindKeyword.KeywordType == NeuKeywordType.Var:

            var hoistedVar = interpreter.HoistVar(kindKeyword.KeywordType, name, initValue);

            if (hoistedVar == null)
            {
                throw new Exception();
            }

            return(hoistedVar);

        ///

        default:

            throw new Exception();
        }
    }
    public static NeuOperation Execute(
        this NeuInterpreter interpreter,
        NeuSource source,
        IEnumerable <IArgument> arguments)
    {
        var codeBlockItemList = source.GetCodeBlockItemList();

        if (codeBlockItemList == null)
        {
            throw new Exception();
        }

        ///

        if (codeBlockItemList.Children.Count() == 0)
        {
            return(NeuOperation.Void);
        }

        ///

        var hasMainFunction = false;

        ///

        var stackOperations = new List <Node>();

        ///

        foreach (var item in codeBlockItemList.Children)
        {
            switch (item)
            {
            case NeuDeclaration decl:

                var result = interpreter.Execute(decl);

                if (result == null)
                {
                    throw new Exception();
                }

                ///

                if (result is NeuFunc func && func.Name == "main")
                {
                    hasMainFunction = true;
                }

                ///

                break;

            ///

            case Node n:

                stackOperations.Add(n);

                break;

            ///

            case var i:

                throw new Exception($"Unexpected: {i}");
            }
        }

        ///

        var lastValue = NeuOperation.Void;

        ///

        foreach (var stackOp in stackOperations)
        {
            lastValue = interpreter.Execute(stackOp);

            ///

            interpreter.SourceOutput.Add(lastValue);
        }

        ///

        if (hasMainFunction)
        {
            var mainCandidates = interpreter.GetOperations("main", null, null, null);

            if (mainCandidates.Count() != 1)
            {
                throw new Exception();
            }

            ///

            var mainFunc = mainCandidates.FirstOrDefault() as NeuFunc;

            if (mainFunc == null)
            {
                throw new Exception();
            }

            ///

            lastValue = interpreter.Execute(mainFunc, Empty <NeuArgument>());

            ///

            interpreter.SourceOutput.Add(lastValue);
        }

        ///

        return(lastValue);
    }