コード例 #1
0
ファイル: CodeRun.cs プロジェクト: afumfer/KNote
 private Expr ValueToExpr(Type t, object newValue)
 {
     if (t == typeof(int))
     {
         IntVal intVal = new IntVal();;
         intVal.Value = Convert.ToInt32(newValue);
         return(intVal);
     }
     else if (t == typeof(float))
     {
         FloatVal floatVal = new FloatVal();
         floatVal.Value = Convert.ToSingle(newValue);
         return(floatVal);
     }
     else if (t == typeof(double))
     {
         DoubleVal doubleVal = new DoubleVal();
         doubleVal.Value = Convert.ToDouble(newValue);
         return(doubleVal);
     }
     else if (t == typeof(decimal))
     {
         DecimalVal decimalVal = new DecimalVal();
         decimalVal.Value = Convert.ToDecimal(newValue);
         return(decimalVal);
     }
     else if (t == typeof(string))
     {
         StringVal stringVal = new StringVal();
         stringVal.Value = Convert.ToString(newValue);
         return(stringVal);
     }
     else if (t == typeof(DateTime))
     {
         DateTimeVal dateTimeVal = new DateTimeVal();
         dateTimeVal.Value = Convert.ToDateTime(newValue);
         return(dateTimeVal);
     }
     else if (t == typeof(bool))
     {
         BoolVal boolVal = new BoolVal();
         boolVal.Value = Convert.ToBoolean(newValue);
         return(boolVal);
     }
     else
     {
         StringVal stringVal = new StringVal();
         stringVal.Value = Convert.ToString(newValue);
         return(stringVal);
     }
 }
コード例 #2
0
        private Expr ParseFactor()
        {
            if (this.index == this.tokens.Count)
            {
                throw new System.Exception("expected expression, got EOF");
            }
            if (this.tokens[this.index] is StringToken)
            {
                StringVal stringLiteral = new StringVal();
                stringLiteral.Value = ((StringToken)this.tokens[this.index]).Value;
                MoveNext();
                return(stringLiteral);
            }
            else if (this.tokens[this.index] is IntToken)
            {
                IntVal intVal = new IntVal();
                intVal.Value = ((IntToken)this.tokens[this.index]).Value;
                MoveNext();
                return(intVal);
            }
            else if (this.tokens[this.index] is FloatToken)
            {
                FloatVal floVal = new FloatVal();
                floVal.Value = ((FloatToken)this.tokens[this.index]).Value;
                MoveNext();
                return(floVal);
            }
            else if (this.tokens[this.index] is DoubleToken)
            {
                DoubleVal douVal = new DoubleVal();
                douVal.Value = ((DoubleToken)this.tokens[this.index]).Value;
                MoveNext();
                return(douVal);
            }
            else if (this.tokens[this.index] is DecimalToken)
            {
                DecimalVal decVal = new DecimalVal();
                decVal.Value = ((DecimalToken)this.tokens[this.index]).Value;
                MoveNext();
                return(decVal);
            }
            else if (this.tokens[this.index] is DateTimeToken)
            {
                DateTimeVal datVal = new DateTimeVal();
                datVal.Value = ((DateTimeToken)this.tokens[this.index]).Value;
                MoveNext();
                return(datVal);
            }
            else if (this.tokens[this.index] is BoolToken)
            {
                BoolVal bolVal = new BoolVal();
                bolVal.Value = ((BoolToken)this.tokens[this.index]).Value;
                MoveNext();
                return(bolVal);
            }
            else if (this.tokens[this.index] == Tokens.Null)
            {
                NullVal nullVal = new NullVal();
                MoveNext();
                return(nullVal);
            }
            else if (this.tokens[this.index] is IdentifierToken)
            {
                string ident = ((IdentifierToken)tokens[index]).Name;

                MoveNext();

                // function expr
                if (MaybeEat(Tokens.LeftBracket))
                {
                    FunctionExpr fun = new FunctionExpr();
                    fun = ParseFunction(ident);
                    return(fun);
                }
                // variable
                else
                {
                    Variable var = new Variable();
                    var.Ident = ident;
                    return(var);
                }
            }
            else if (this.tokens[this.index] == Tokens.New)
            {
                MoveNext(); // eat new
                return(ParseNewObject());
            }
            else if (this.tokens[this.index] == Tokens.LeftBracket)
            {
                // Eat LeftParenthesis
                MoveNext();
                Expr result = ParseExpr();
                Eat(Tokens.RightBracket);
                return(result);
            }
            else if (IsUnaryOperator(this.tokens[index]))
            {
                // Unary Expression
                UnaryExpr result = new UnaryExpr();

                if (MaybeEat(Tokens.Sub))
                {
                    result.Op = TokenToBinOp(Tokens.Sub);
                }
                else if (MaybeEat(Tokens.Add))
                {
                    result.Op = TokenToBinOp(Tokens.Add);
                }
                else if (MaybeEat(Tokens.Not))
                {
                    result.Op = TokenToBinOp(Tokens.Not);
                }
                else
                {
                    throw new System.Exception(string.Format(
                                                   "Operator '{0}' is not supported in unary expressions",
                                                   tokens[index]));
                }

                result.Expression = ParseFactor();
                return(result);
            }
            else
            {
                throw new System.Exception("expected string literal, numeric literal, or variable");
            }
        }
コード例 #3
0
ファイル: CodeRun.cs プロジェクト: afumfer/KNote
        private void RunStmt(Stmt stmt)
        {
            if (flagBreak)
            {
                return;
            }

            //Application.DoEvents();

            #region Sequence

            if (stmt is Sequence)
            {
                Sequence seq = (Sequence)stmt;
                RunStmt(seq.First);
                RunStmt(seq.Second);
            }

            #endregion

            #region DeclareVar

            else if (stmt is DeclareVar)
            {
                // declare
                DeclareVar declare = (DeclareVar)stmt;

                CodeDeclareSymbol(declare);

                //TODO: (Z) Sustituir lo anterior por esto cuando se
                //       arregle el código de asignación + declaración.
                //Assign assign = new Assign();
                //assign.Ident = declare.Ident;
                //assign.Expr = declare.Expr;
                //RunStmt(assign);
            }

            #endregion

            #region Assign

            else if (stmt is Assign)
            {
                Assign assign = (Assign)stmt;
                CodeStoreSymbol(assign);
            }

            #endregion

            #region Print / PrintLine / Clear

            else if (stmt is Print)
            {
                Print print = (Print)stmt;
                CodeExecutePrint(print);
            }

            else if (stmt is PrintLine)
            {
                PrintLine printLine = (PrintLine)stmt;
                CodeExecutePrintLine(printLine);
            }

            else if (stmt is Clear)
            {
                CodeExecuteClear();
            }

            #endregion

            #region FunctionStmt

            else if (stmt is FunctionStmt)
            {
                FunctionStmt fun = (FunctionStmt)stmt;
                CodeExecuteFunction(fun.Function);
            }

            #endregion

            #region ReadVar

            else if (stmt is ReadVar)
            {
                ReadVar            read = (ReadVar)stmt;
                ReadVarItem        readVarItem;
                Assign             assign;
                List <ReadVarItem> readVarItmes = new List <ReadVarItem>();

                foreach (var pair in read.Vars)
                {
                    readVarItem          = new ReadVarItem();
                    readVarItem.VarIdent = pair.Key.Ident;
                    readVarItem.VarValue = CodeReadSymbol(pair.Key);
                    readVarItem.Label    = GenExpr(pair.Value);
                    readVarItmes.Add(readVarItem);
                }

                if (CodeExecuteReadVars(readVarItmes))
                {
                    foreach (ReadVarItem vi in readVarItmes)
                    {
                        assign = new Assign();
                        //assign.Ident = vi.Var.Ident;
                        assign.Ident = vi.VarIdent;
                        assign.Expr  = ValueToExpr(vi.VarValue.GetType(), vi.VarNewValueText);
                        RunStmt(assign);
                    }
                }
            }

            #endregion

            #region BreakStmt

            else if (stmt is BreakStmt)
            {
                flagBreak = true;
                return;
            }

            #endregion

            #region  FoorLoop

            else if (stmt is ForLoop)
            {
                // example:
                // for x = 0 to 100
                //   print "hello";
                // end for;

                ForLoop forLoop = (ForLoop)stmt;

                IntVal numFrom = new IntVal();
                IntVal numTo   = new IntVal();

                Assign assignFrom = new Assign();
                assignFrom.Ident = forLoop.Ident;

                assignFrom.Expr = forLoop.From;
                RunStmt(assignFrom);

                numFrom.Value = Convert.ToInt32(GenExpr(forLoop.From));
                numTo.Value   = Convert.ToInt32(GenExpr(forLoop.To));

                while (numFrom.Value <= numTo.Value)
                {
                    if (flagBreak)
                    {
                        break;
                    }
                    RunStmt(forLoop.Body);
                    numFrom.Value++;
                    assignFrom.Expr = numFrom;
                    RunStmt(assignFrom);
                }
                if (flagBreak)
                {
                    flagBreak = false;
                }
            }

            #endregion

            #region FoorEachLoop

            else if (stmt is ForEachLoop)
            {
                // example:
                // foreach x in myColec
                //   print "hello";
                //   print x.MyProp;
                // end foreach;

                ForEachLoop forEachLoop = (ForEachLoop)stmt;

                object colec = GenExpr(forEachLoop.Colec);

                foreach (object o in (IEnumerable <object>)colec)
                {
                    if (flagBreak)
                    {
                        break;
                    }

                    // TODO: Pending susutiruir CodeSpecialStoreObject by CodeStoreSymbol
                    //       In the future, CodeStoreSymbol should store the variable
                    //       if it had not previously been declared.
                    CodeSpecialStoreObject(forEachLoop.Ident, o);
                    // CodeStoreSymbol(forEachLoop.Ident, o);

                    RunStmt(forEachLoop.Body);
                }
                if (flagBreak)
                {
                    flagBreak = false;
                }
            }

            #endregion

            #region IfStmt

            else if (stmt is IfStmt)
            {
                // example:
                // if a == 10 then
                //   print "hello";
                // else
                //   print "bye";
                // end if;

                IfStmt ifStmt = (IfStmt)stmt;
                IntVal ifExp  = new IntVal();

                ifExp.Value = Convert.ToInt32(GenExpr(ifStmt.TestExpr));

                if (ifExp.Value != 0)
                {
                    RunStmt(ifStmt.BodyIf);
                }
                else
                {
                    if (ifStmt.DoElse)
                    {
                        RunStmt(ifStmt.BodyElse);
                    }
                }
            }

            #endregion

            #region WhileStmt

            else if (stmt is WhileStmt)
            {
                // example:
                // while a <= 10
                //   print "hello";
                //   a = a + 1;
                // end while;

                WhileStmt whileStmt = (WhileStmt)stmt;
                IntVal    whileExp  = new IntVal();

                while (true)
                {
                    if (flagBreak)
                    {
                        break;
                    }
                    whileExp.Value = Convert.ToInt32(GenExpr(whileStmt.TestExpr));
                    if (whileExp.Value == 0)
                    {
                        break;
                    }
                    RunStmt(whileStmt.Body);
                }
                if (flagBreak)
                {
                    flagBreak = false;
                }
            }

            #endregion

            else
            {
                throw new System.Exception("don't know how to gen a " + stmt.GetType().Name);
            }
        }