Exemplo n.º 1
0
            public IfStatement Parse()
            {
                PeekToken();

                if (!Test(Token.Type.If, IfState.PreLeftPar) ||
                    !Test(Token.Type.LeftPar, IfState.PreExpr))
                {
                    return(null);
                }
                PeekToken(IfState.PostExpr);
                var cond = lastToken.GetData <IBaseExpression>();

                Expect(Token.Type.RightPar, IfState.PreBlock);
                PeekToken(IfState.PostBlock);
                IBaseStatement
                    ifBlock   = lastToken.GetData <IBaseStatement>(),
                    elseBlock = null;

                if (Test(Token.Type.Else, IfState.PostElse))
                {
                    CommitPeek();
                    elseBlock = lastToken.GetData <IBaseStatement>();
                }
                else
                {
                    SetState(IfState.End);
                }

                return(new IfStatement(cond, ifBlock, elseBlock));
            }
Exemplo n.º 2
0
            public DelegateT Parse()
            {
                var method = new DynamicMethod(
                    "Evaluate",
                    typeof(long),
                    new Type[] { typeof(long), typeof(long), typeof(long) }
                    );
                var generator = method.GetILGenerator();


                PeekToken();
                if (TestNoShift(Token.Type.VarDefStatement))
                {
                    VarDefStatement defStt = token.GetData <VarDefStatement>();
                    defStt.Emit(generator);
                    int i = 0;
                    defStt.names.ForEach(name => vars[name] = new Variable(name, i++, true));
                    PeekToken(ProgramState.AfterDef);
                }
                foreach (var(name, obj) in globalVars)
                {
                    if (!vars.ContainsKey(name))
                    {
                        vars[name] = obj;
                    }
                }

                IBaseStatement stt     = null;
                bool           returns = false;

                while (HasToken())
                {
                    stt      = token.GetData <IBaseStatement>();
                    returns |= stt.CheckReturn();
                    stt.Emit(generator);
                    CommitPeek();
                    if (lexer.Source.CommitAndCheckIsEnd())
                    {
                        break;
                    }
                    RePeekToken(ProgramState.AfterDef);
                }
                if (!returns)
                {
                    throw new CompileException(
                              "end of function is reachable without any return statement"
                              );
                }
                if (!(stt is ReturnStatement))
                {
                    new ReturnStatement(new Const(0)).Emit(generator); // load dummy to not get error
                }
                return((DelegateT)method.CreateDelegate(typeof(DelegateT)));
            }
Exemplo n.º 3
0
 public IfStatement(
     IBaseExpression condition,
     IBaseStatement ifBranch,
     IBaseStatement elseBranch = null
     )
 {
     IBaseExpression.CheckTypes(
         "if condition", "",
         typeof(bool), condition.GetResultType()
         );
     this.condition  = condition;
     precalc         = (bool?)condition.Evaluate();
     this.ifBranch   = ifBranch;
     this.elseBranch = elseBranch;
 }