public void InterpretType(IdentifierExpression identifierExp, ObjectExpression objectExp)
        {
            var name = identifierExp.Identifier;
            var offset = 0;

            var fields = objectExp.Pairs
                .Select(x => x.LeftOperand)
                .Cast<IdentifierExpression>()
                .Select(x =>
                {
                    var type = GetType(x);
                    var size = ResolveTypeSize(type);
                    var ofs = offset;
                    offset += size;

                    return new StructMember(x.Identifier, type, ofs, size);
                })
                .ToArray();

            var structSize = fields.Sum(x => x.Size);
            var aphidStruct = new AphidStruct(name, fields, structSize);

            if (!_types.ContainsKey(name))
            {
                _types.Add(name, aphidStruct);
            }
            else
            {
                _types[name] = aphidStruct;
            }
        }
        public List<AphidExpression> ExpandIfExpression(IfExpression expression)
        {
            var g = Guid.NewGuid();

            IdentifierExpression ifLabel = new IdentifierExpression("If_" + g),
                elseLabel = new IdentifierExpression("Else_" + g),
                endIfLabel = new IdentifierExpression("EndIf_" + g);

            var ast = new List<AphidExpression>
            {
                ifLabel,
                MutateCondition(expression.Condition),
                new CallExpression(gotoFalseId, elseLabel)
            };

            ast.AddRange(expression.Body);
            ast.Add(new CallExpression(gotoId, endIfLabel));
            ast.Add(elseLabel);

            if (expression.ElseBody != null)
            {
                ast.AddRange(expression.ElseBody);
            }

            ast.Add(endIfLabel);

            return ast;
        }
示例#3
0
        public IdentifierExpression ToIdentifierExpression()
        {
            var idExp = new IdentifierExpression(Name);
            var attrs = new List<IdentifierExpression>();

            if (IsRoot)
            {
                attrs.Add(new IdentifierExpression("root"));
            }

            if (IsOptional)
            {
                attrs.Add(new IdentifierExpression("opt"));
            }

            if (IsList)
            {
                attrs.Add(new IdentifierExpression("list"));
            }

            if (Type != null)
            {
                attrs.Add(new IdentifierExpression(Type));
            }

            idExp.Attributes.AddRange(attrs);

            return idExp;
        }
示例#4
0
        public static ParserIdentifier FromIdentifierExpression(IdentifierExpression identifierExpression)
        {
            var id = AphidAttributeParser.Parse<ParserIdentifier>(identifierExpression);
            id.Name = identifierExpression.Identifier;

            return id;
        }
        protected override List<AphidExpression> MutateCore(AphidExpression expression, out bool hasChanged)
        {
            hasChanged = false;

            if (!IsStatement || expression.Type != AphidNodeType.IdentifierExpression)
            {
                return null;
            }

            var id = expression.ToIdentifier();
            var attributes = AphidAttributeParser.Parse<DeclarativeStatementAttributes>(id);
            var nameId = new IdentifierExpression(attributes.Name);

            if (_tokenTypes.Contains(id.Identifier))
            {
                hasChanged = true;

                var match = new CallExpression(
                    new IdentifierExpression("Match"),
                    new IdentifierExpression(id.Identifier));

                return attributes.Name == null ?
                    new List<AphidExpression> { match, } :
                    new List<AphidExpression>
                    {
                        new BinaryOperatorExpression(
                            nameId,
                            AphidTokenType.AssignmentOperator,
                            new IdentifierExpression("TokenType")),
                        match,
                    };
            }
            else if (_parseFunctions.Contains(id.Identifier))
            {
                hasChanged = true;
                var call = new CallExpression(new IdentifierExpression(id.Identifier));

                return attributes.Name == null ?
                    new List<AphidExpression> { call } :
                    new List<AphidExpression>
                    {
                        new BinaryOperatorExpression(
                            nameId,
                            AphidTokenType.AssignmentOperator,
                            call)
                    };
            }
            else if (id.Identifier == "NextToken")
            {
                hasChanged = true;

                return new List<AphidExpression>
                {
                    new CallExpression(new IdentifierExpression(id.Identifier))
                };
            }

            return null;
        }
示例#6
0
 public TryExpression(
     List<AphidExpression> tryBody,
     IdentifierExpression catchArg,
     List<AphidExpression> catchBody,
     List<AphidExpression> finallyBody)
 {
     TryBody = tryBody;
     CatchBody = catchBody;
     CatchArg = catchArg;
     FinallyBody = finallyBody;
 }
        public void AddLabel(IdentifierExpression exp)
        {
            _labels.Add(exp.Identifier, _currentAddress);

            var needed = _needLabelAddresses.Where(x => x.Key == exp.Identifier).ToArray();
            foreach (var inst in needed)
            {
                GetBytes(_currentAddress).CopyTo(inst.Value, 1);
            }

            foreach (var n in needed)
            {
                _needLabelAddresses.Remove(n);
            }
        }
        public List<AphidExpression> ExpandWhileExpression(ControlFlowExpression expression)
        {
            var g = Guid.NewGuid();

            IdentifierExpression whileLabel = new IdentifierExpression("While_" + g),
                endWhileLabel = new IdentifierExpression("EndWhileIf_" + g);

            var ast = new List<AphidExpression>
            {
                whileLabel,
                MutateCondition(expression.Condition),
                new CallExpression(gotoFalseId, endWhileLabel),
            };

            ast.AddRange(expression.Body);
            ast.Add(new CallExpression(gotoId, whileLabel));
            ast.Add(endWhileLabel);

            return ast;
        }
示例#9
0
 private AphidExpression ParseMemberExpression()
 {
     var factor = ParseFactorCallExpression();
     for (
     ; (this._currentToken.TokenType == AphidTokenType.MemberOperator);
     ) {
         NextToken();
         AphidExpression exp = default(AphidExpression);
         if ((this._currentToken.TokenType == AphidTokenType.Identifier)) {
             exp = new IdentifierExpression(this._currentToken.Lexeme);
             NextToken();
         }
         else {
             if ((this._currentToken.TokenType == AphidTokenType.String)) {
                 exp = ParseStringExpression();
             }
             else {
                 if ((this._currentToken.TokenType == AphidTokenType.LeftBrace)) {
                     NextToken();
                     exp = new DynamicMemberExpression(ParseExpression());
                     Match(AphidTokenType.RightBrace);
                 }
                 else {
                     throw new AphidParserException(_currentToken);
                 }
             }
         }
         factor = new BinaryOperatorExpression(factor, AphidTokenType.MemberOperator, exp);
         for (
         ; (this._currentToken.TokenType == AphidTokenType.LeftParenthesis);
         ) {
             NextToken();
             if ((this._currentToken.TokenType == AphidTokenType.RightParenthesis)) {
                 NextToken();
                 factor = new CallExpression(factor);
             }
             else {
                 var args = ParseTuple();
                 Match(AphidTokenType.RightParenthesis);
                 factor = new CallExpression(factor, args);
             }
         }
         if ((this._currentToken.TokenType == AphidTokenType.definedKeyword)) {
             NextToken();
             return new UnaryOperatorExpression(AphidTokenType.definedKeyword, factor, true);
         }
     }
     return factor;
 }
示例#10
0
        private AphidObject InterpretIdentifierExpression(IdentifierExpression expression)
        {
            AphidObject obj;

            if (_currentScope.TryResolve(expression.Identifier, out obj))
            {
                return obj;
            }
            else
            {
                return null;
            }
        }
示例#11
0
        private CodeExpression GeneratePeek(IdentifierExpression node, out string varName)
        {
            if (node.Attributes.Count > 1)
            {
                throw new NotImplementedException();
            }

            varName = node.Attributes.Any() ? node.Attributes.First().Identifier : null;
            var tokenName = node.Identifier;

            var exp = CodeHelper.BinOpExp(
                GetCurrentTokenType(),
                CodeBinaryOperatorType.ValueEquality,
                CodeHelper.FieldRef(CodeHelper.TypeRefExp("AphidTokenType"), tokenName));

            return exp;
        }
示例#12
0
        private CodeStatementCollection GenerateParseStatement(IdentifierExpression node)
        {
            string typeName = node.Identifier,
                parseFuncName = _parsePrefix + node.Identifier,
                localName = null;

            var invokeExp = CodeHelper.Invoke(parseFuncName);
            var statements = new List<CodeStatement>();

            if (node.Attributes.Any())
            {
                localName = node.Attributes.First().Identifier;
                //localName = localName.ToLower().Substring(0, 1) + localName.Substring(1);
                _locals.Add(localName, typeName);
                var stmt = CodeHelper.Assign(localName, invokeExp);
                statements.Add(stmt);
                statements.Add(CodeHelper.Assign(_nodeLocal, CodeHelper.VarRef(localName)));
            }
            else
            {
                var stmt = CodeHelper.Assign(_nodeLocal, invokeExp);
                statements.Add(stmt);
            }

            return new CodeStatementCollection(statements.ToArray());
        }
示例#13
0
        private CodeStatementCollection GenerateMatchStatement(IdentifierExpression node)
        {
            if (node.Attributes.Count > 1)
            {
                throw new NotImplementedException();
            }

            if (ResolveType(node.Identifier) == ReferenceType.RuleDeclaration)
            {
                return GenerateParseStatement(node);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
示例#14
0
        private CodeStatementCollection GenerateImperativeStatement(IdentifierExpression node)
        {
            var parserId = ParserIdentifier.FromIdentifierExpression(node);

            if (parserId.Name != "Error")
            {
                var typeRef = CodeHelper.TypeRef(parserId.Type ?? _config.BaseClass);
                typeRef = parserId.IsList ? GetListTypeRef(typeRef) : typeRef;
                var init = new CodeDefaultValueExpression(typeRef);
                var varDecl = new CodeVariableDeclarationStatement(typeRef, node.Identifier, init);
                _scope.Add(node.Identifier, new AphidObject());

                return new CodeStatementCollection(new[] { varDecl });
            }
            else
            {
                return new CodeStatementCollection(new[]
                {
                    new CodeThrowExceptionStatement(
                        new CodeObjectCreateExpression(
                            _config.ExceptionClass,
                            CodeHelper.VarRef("_currentToken")))
                });
            }
        }
示例#15
0
 private Expression ParseIdentifierExpression()
 {
     var exp = new IdentifierExpression(_currentToken.Lexeme);
     NextToken();
     return exp;
 }
 public string GetType(IdentifierExpression identifier)
 {
     return identifier.Attributes.Any() ?
         identifier.Attributes.Single().Identifier :
         _dword;
 }
        private AphidObject InterpretIdentifierExpression(IdentifierExpression expression, AphidScope scope = null)
        {
            if (scope == null)
            {
                scope = _currentScope;
            }

            AphidObject obj;
            if (scope.Variables.TryGetValue(expression.Identifier, out obj))
            {
                return obj;
            }
            else if (scope.Parent != null)
            {
                return InterpretIdentifierExpression(expression, scope.Parent);
            }
            else
            {
                return null;
            }
        }
示例#18
0
 private IdentifierExpression ParseIdentifierExpression()
 {
     var exp = new IdentifierExpression(this._currentToken.Lexeme);
     NextToken();
     if ((this._currentToken.TokenType == AphidTokenType.Identifier)) {
         var id = exp;
         var attrs = new System.Collections.Generic.List<IdentifierExpression>();
         for (
         ; true;
         ) {
             attrs.Add(id);
             id = new IdentifierExpression(this._currentToken.Lexeme);
             NextToken();
             if (((this._currentToken.TokenType == AphidTokenType.Identifier)
                         == false)) {
     break;
             }
         }
         exp = new IdentifierExpression(id.Identifier, attrs);
     }
     return exp;
 }
        public AphidStruct ResolveType(IdentifierExpression identifier)
        {
            var type = GetType(identifier);

            return ResolveType(type);
        }
示例#20
0
        private ParserIdentifier ParseMethodAttributes(IdentifierExpression node)
        {
            var attrs = AphidAttributeParser.Parse<ParserIdentifier>(node);

            if (attrs.Type == null)
            {
                attrs.Type = _config.BaseClass;
            }

            return attrs;
        }
示例#21
0
        private ParserIdentifier ParseRuleProperty(IdentifierExpression propertyId)
        {
            var prop = AphidAttributeParser.Parse<ParserIdentifier>(propertyId);
            prop.Name = propertyId.Identifier;

            if (prop.Type == null)
            {
                prop.Type = _config.BaseClass;
            }

            return prop;
        }
示例#22
0
        public Expression ParseMemberExpression()
        {
            Expression factor = ParseCallExpression(ParseFactor());

            while (_currentToken.TokenType == AphidTokenType.MemberOperator)
            {
                NextToken();

                Expression exp;

                switch (_currentToken.TokenType)
                {
                    case AphidTokenType.Identifier:
                        exp = new IdentifierExpression(_currentToken.Lexeme);
                        NextToken();
                        break;

                    case AphidTokenType.String:
                        exp = ParseStringExpression();
                        break;

                    case AphidTokenType.LeftBrace:
                        NextToken();
                        exp = new DynamicMemberExpression(ParseExpression());
                        Match(AphidTokenType.RightBrace);
                        break;

                    default:
                        throw new AphidParserException(_currentToken);
                }

                factor = ParseCallExpression(new BinaryOperatorExpression(factor, AphidTokenType.MemberOperator, exp));
            }

            return factor;
        }
示例#23
0
        private CodeExpression GenerateImperativeExpression(IdentifierExpression node, bool isCondition = false)
        {
            switch (node.Identifier)
            {
                case _currentTokenType:
                    return GetCurrentTokenType();

                case _currentLexeme:
                    return GetCurrentLexeme();

                default:
                    if (IsTokenType(node.Identifier))
                    {
                        var exp = (CodeExpression)GetTokenTypeRef(node.Identifier);

                        if (!isCondition)
                        {
                            return exp;
                        }
                        else
                        {
                            return CodeHelper.BinOpExp(
                                GetCurrentTokenType(),
                                CodeBinaryOperatorType.ValueEquality,
                                exp);
                        }
                    }
                    else if (node.Attributes.Any())
                    {
                        var attr = node.Attributes.Single().Identifier;

                        if (attr == "list")
                        {
                            return new CodeObjectCreateExpression(
                                GetListTypeRef(CodeHelper.TypeRef(node.Identifier)));
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    else
                    {
                        return CodeHelper.VarRef(node.Identifier);
                    }

            }
        }
示例#24
0
        private BinaryOperatorExpression ParseKeyValuePairExpression()
        {
            var id = new IdentifierExpression(_currentToken.Lexeme);
            NextToken();
            Expression exp;

            if (_currentToken.TokenType == AphidTokenType.ColonOperator)
            {
                NextToken();
                exp = ParseExpression();
            }
            else
            {
                exp = id;
            }

            return new BinaryOperatorExpression(id, AphidTokenType.ColonOperator, exp);
        }
示例#25
0
        private IdentifierExpression ParseIdentifierExpression()
        {
            var exp = new IdentifierExpression(_currentToken.Lexeme);
            NextToken();

            if (_currentToken.TokenType == AphidTokenType.Identifier)
            {
                var id = exp;
                var attributes = new List<IdentifierExpression>();

                do
                {
                    attributes.Add(id);
                    id = new IdentifierExpression(_currentToken.Lexeme);
                    NextToken();
                }
                while (_currentToken.TokenType == AphidTokenType.Identifier);

                id.Attributes = attributes;
                exp = id;
            }

            return exp;
        }