示例#1
0
        public Ast_Base Parse(ParserState state)
        {
            var      token = Expect(TokenType.Identifier, state);
            var      ident = ParseIdentifier(token, state);
            Ast_Base ast   = ident.Type switch
            {
                AstType.Call => ParseCall(ident, state),
                AstType.Keyword => ParseKeyword(ident, state),
                AstType.Variable => ParseAssign(ident, state),
                AstType.Lambda => ParseLambda(ident, state),
                _ => throw new SyntaxError(ident.Token, $"Unhandled ast type ({ident.Type}) found."),
            };

            return(ast);
        }
示例#2
0
        public Ast_Base ParseAssign(Ast_Base ident, ParserState state)
        {
            var token = state.GetToken();
            var ast   = new Ast_Assign(token)
            {
                Variable   = ident as Ast_Variable,
                Operand    = token,
                Expression = ParseExpression(TokenType.Semicolon, state)
            };
            var ve = state.Scope.VariableExists(ast.Variable.Name);

            if (!ve)
            {
                state.Scope.Variables.Add(ast.Variable);
                if (ast.Expression?.Token?.Lexeme.ToString() == Ast_Variable.RecordValue)
                {
                    ast.Variable.SetValue(new Token()
                    {
                        Lexeme = Ast_Variable.NewRecordValue, Type = TokenType.TypeRecord
                    });
                }
                else if (ast.Expression?.Token?.Lexeme.ToString() == Ast_Variable.ArrayValue)
                {
                    ast.Variable.SetValue(new Token()
                    {
                        Lexeme = Ast_Variable.NewArrayValue, Type = TokenType.TypeArray
                    });
                }
                else if (ast.Expression?.Token?.Lexeme.ToString() == Ast_Variable.ParamsValue)
                {
                    ast.Variable.SetValue(new Token()
                    {
                        Lexeme = Ast_Variable.NewParamsValue, Type = TokenType.TypeParams
                    });
                }
                else if (Ast_Expression.Constants.Contains(ast.Expression.Token.Type))
                {
                    ast.Variable.SetValue(ast.Expression.Token);
                }
                else
                {
                    ast.Variable.SetValue(token: new Token()
                    {
                        Lexeme = ast.Expression.ToString(), Type = TokenType.Expression
                    });
                }
            }
            else
            {
                var v = state.Scope.GetVariable(ast.Variable.Name);
                if (v == null)
                {
                    throw new SyntaxError(token, $"Variable not found {ast.Variable.Name}.");
                }
                if (v.Value.Type == ValueType.Record && ast.Variable.Index?.Block.Count > 0 && ast.Variable.Index.Block[^ 1].Type == AstType.NewArrayIndex)
                {
                    throw new SyntaxError(token, "Invalid record index, no index was provided.");
                }
                if ((v.Value.Type == ValueType.Array || v.Value.Type == ValueType.Record || v.Value.Type == ValueType.Params) && ast.Variable.Index == null)
                {
                    var l = ast.Expression.Block[^ 1].Token.Lexeme;