コード例 #1
0
ファイル: Parser.cs プロジェクト: strunberg/TEA
        private bool ParseFactorExpression(TokenReader reader, out Expression result)
        {
            Token tok = reader.Peek();
            if (tok.Is(Keyword.LeftParen))
            {
                reader.Read();
                if (!this.ParseExpression(reader, out result))
                {
                    return false;
                }

                if (!this.Expect(reader, Keyword.RightParen))
                {
                    return false;
                }

                return true;
            }

            if (tok.Is(Keyword.Not))
            {
                return this.ParseNotExpression(reader, out result);
            }

            if (tok.Is(Keyword.Minus))
            {
                return this.ParseNegativeExpression(reader, out result);
            }

            if (tok.Is(Keyword.New))
            {
                return this.ParseNewExpression(reader, out result);
            }

            if (tok.Is(Keyword.Nil))
            {
                reader.Read();
                result = new LiteralExpression(tok, null);
                return true;
            }

            if (tok.Is(Keyword.True))
            {
                reader.Read();
                result = new LiteralExpression(tok, true);
                return true;
            }

            if (tok.Is(Keyword.False))
            {
                reader.Read();
                result = new LiteralExpression(tok, false);
                return true;
            }

            LiteralToken literalTok = tok as LiteralToken;
            if (literalTok != null)
            {
                result = new LiteralExpression(tok, literalTok.Value);
                reader.Read();
                return true;
            }

            if (tok.Is(Keyword.Address))
            {
                return this.ParseAddressExpression(reader, out result);
            }

            if (tok is IdentifierToken || tok.Is(Keyword.Inherited))
            {
                ReferenceExpression refExpr = null;
                if (!this.ParseReferenceExpression(reader, out refExpr))
                {
                    result = null;
                    return false;
                }

                result = refExpr;
                return true;
            }

            string message = string.Format(
                System.Globalization.CultureInfo.CurrentCulture,
                Properties.Resources.Parser_Unexpected,
                tok != null ? tok.ToString() : Properties.Resources.Parser_EndOfFile);
            this.log.Write(new Message(reader.Path, reader.Line, reader.Column, Severity.Error, message));
            result = null;
            return false;
        }
コード例 #2
0
ファイル: CodeGenerator.cs プロジェクト: strunberg/TEA
        private bool TryEmitLiteralExpression(
            LiteralExpression expression,
            CompilerContext context,
            Scope scope,
            MethodImpl method,
            out TypeDefinition valueType)
        {
            if (expression.Value == null)
            {
                method.Statements.Add(new AsmStatement { Instruction = "xor eax,eax" });
                return context.TryFindTypeByName("^", out valueType);
            }

            if (expression.Value is int)
            {
                if ((int)expression.Value == 0)
                {
                    method.Statements.Add(new AsmStatement { Instruction = "xor eax,eax" });
                }
                else
                {
                    method.Statements.Add(new AsmStatement { Instruction = string.Format("mov eax,{0}", expression.Value) });
                }

                return context.TryFindTypeByName("integer", out valueType);
            }

            if (expression.Value is char)
            {
                method.Statements.Add(new AsmStatement { Instruction = string.Format("mov eax,{0}", (int)expression.Value) });
                return context.TryFindTypeByName("character", out valueType);
            }

            if (expression.Value is string)
            {
                string label = method.Module.DefineLiteralString((string)expression.Value);
                method.Statements.Add(new AsmStatement { Instruction = string.Format("lea eax,[offset {0}]", label) });
                return context.TryFindTypeByName("#0character", out valueType);
            }

            if (expression.Value is decimal)
            {
                decimal exprValue = (decimal)expression.Value;
                if (exprValue == 0.0M)
                {
                    method.Statements.Add(new AsmStatement { Instruction = "fldz" });
                }
                else if (exprValue == 1.0M)
                {
                    method.Statements.Add(new AsmStatement { Instruction = "fld1" });
                }
                else
                {
                    string label = method.Module.DefineConstant((double)exprValue);
                    method.Statements.Add(new AsmStatement { Instruction = string.Format("fld qword ptr [{0}]", label) });
                }
                return context.TryFindTypeByName("double", out valueType);
            }

            if (expression.Value is bool)
            {
                bool boolVal = (bool)expression.Value;
                if (boolVal)
                {
                    method.Statements.Add(new AsmStatement { Instruction = "mov eax,1" });
                }
                else
                {
                    method.Statements.Add(new AsmStatement { Instruction = "xor eax,eax" });
                }

                return context.TryFindTypeByName("boolean", out valueType);
            }

            valueType = null;
            return false;
        }