public void SyntaxOKTest () { parser = new Parser ("var } function + i 'Hello',".ToCharArray ()); List<Comment> comments = new List<Comment> (); parser.ParseProgram (ref comments); Assert.IsFalse (parser.SyntaxOK ()); }
public void BlockTest () { parser = new Parser ("{}".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (BlockStatement), it.Element, "#3.1"); Assert.IsTrue (parser.SyntaxOK ()); }
public void VarTest () { parser = new Parser ("var a = 10;".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement > list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement,BlockStatement>.Iterator(list); Assert.IsInstanceOfType (typeof(VariableDeclarationStatement), it.Element, "#1.1"); Assert.IsTrue (parser.SyntaxOK ()); }
public void FunctionTest () { parser = new Parser ("function foo() {}".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (FunctionStatement), it.Element, "#2.1"); FunctionStatement foo = ((FunctionStatement)it.Element); Assert.AreEqual ("foo", foo.Function.Name.Spelling, "#2.2"); Assert.IsTrue (parser.SyntaxOK ()); }
public SLE.LambdaExpression CompileExpression (char[] Input, ref List<Diagnostic> Diagnostics) { IdentifierMappingTable idmtable = new IdentifierMappingTable (); IdentifierTable idtable = new IdentifierTable (); Parser parser = new Parser (Input, idtable, true); List<Comment> comments = null; BindingInfo bindinginfo = null; Expression expr = parser.ParseExpression (ref comments, ref bindinginfo); Diagnostics = parser.Diagnostics; RowanGenerator gen = new RowanGenerator (idmtable, idtable.InsertIdentifier("this"), idtable.InsertIdentifier("arguments")); return gen.BindAndTransform (expr, bindinginfo); }
public SLE.LambdaExpression CompileProgram (char[] Input, ref List<Diagnostic> Diagnostics, ref bool IncompleteInput, bool PrintExpressions) { IdentifierMappingTable idmtable = new IdentifierMappingTable (); IdentifierTable idtable = new IdentifierTable (); Parser parser = new Parser (Input, new IdentifierTable (), true);//tode get ecma from somewhere List<Comment> comments = null; BindingInfo bindinginfo = null; DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments, ref bindinginfo); Diagnostics = parser.Diagnostics; IncompleteInput = parser.SyntaxIncomplete(); RowanGenerator gen = new RowanGenerator (idmtable, idtable.InsertIdentifier ("this"), idtable.InsertIdentifier ("arguments")); return gen.BindAndTransform (list, bindinginfo, PrintExpressions); }
public void GenerateFunctionDefinition () { Parser parser = new Parser ("function foo() {}".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Statement input = parser.ParseStatement (ref comms); //seems to be important to init the globals else it bug everywhere!! gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); Assert.IsInstanceOfType (typeof (MJCP.ExpressionStatement), input, "#1"); MJCP.Expression exp = ((MJCP.ExpressionStatement)input).Expression; Assert.IsInstanceOfType (typeof (MJCP.FunctionExpression), exp, "#2"); MJCP.FunctionDefinition def = ((MJCP.FunctionExpression)exp).Function; MSIA.Statement result = gen.Generate (def); Assert.IsInstanceOfType (typeof (MSIA.ExpressionStatement), result, "#3"); MSIA.ExpressionStatement sta = (MSIA.ExpressionStatement) result; Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), sta.Expression, "#4"); MSIA.BoundAssignment boundexp = ((MSIA.BoundAssignment)sta.Expression); Assert.IsInstanceOfType (typeof (MSIA.MethodCallExpression), boundexp.Value, "#5"); MSIA.MethodCallExpression call = (MSIA.MethodCallExpression)boundexp.Value; Assert.AreEqual (7, call.Arguments.Count, "#6"); Assert.IsInstanceOfType (typeof (MSIA.CodeContextExpression), call.Arguments[0], "#7.1"); Assert.IsInstanceOfType (typeof (MSIA.ConstantExpression), call.Arguments[1], "#7.2"); Assert.AreEqual ("foo", ((MSIA.ConstantExpression) call.Arguments[1]).Value, "#7.2.1"); Assert.IsInstanceOfType (typeof (MSIA.ConstantExpression), call.Arguments[2], "#7.3"); Assert.AreEqual (-1, ((MSIA.ConstantExpression)call.Arguments[2]).Value, "#7.3.1"); Assert.IsInstanceOfType (typeof (MSIA.CodeBlockExpression), call.Arguments[3], "#7.4"); //BODY is test with statement test so not needed //Assert.IsInstanceOfType (typeof (MSIA.CodeBlockExpression), ((MSIA.CodeBlockExpression)call.Arguments[3]).Block.Body, "#7.4.1"); Assert.AreEqual ("_", ((MSIA.CodeBlockExpression)call.Arguments[3]).Block.Name, "#7.4.2"); Assert.IsFalse (((MSIA.CodeBlockExpression)call.Arguments[3]).Block.ParameterArray, "#7.4.3"); //todo test parameters //Assert.IsNull (((MSIA.CodeBlockExpression)call.Arguments[3]).Block.Parameters, "#7.4.4"); Assert.IsInstanceOfType (typeof (MSIA.NewArrayExpression), call.Arguments[4], "#7.5"); //Assert.IsInstanceOfType (typeof (string[]), ((MSIA.NewArrayExpression)call.Arguments[4]).ExpressionType, "#7.5.1"); Assert.IsInstanceOfType (typeof (MSIA.ConstantExpression), call.Arguments[5], "#7.6"); Assert.IsInstanceOfType (typeof (MSIA.ConstantExpression), call.Arguments[6], "#7.7"); MSIA.Expression expr = call.Arguments[0]; }
public void GenerateQualified () { Parser parser = new Parser ("this.foo;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.ActionExpression), exp, "#1"); Assert.IsInstanceOfType (typeof (MSA.GetMemberAction), ((MSIA.ActionExpression)exp).Action, "#2"); }
public void AddTest () { Parser parser = new Parser ("var i = 5 + 1;".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (VariableDeclarationStatement), it.Element, "#8.1"); VariableDeclarationStatement varst = ((VariableDeclarationStatement)it.Element); Assert.AreEqual ("i", varst.Declarations[0].Declaration.Name.Spelling, "#8.2"); Assert.IsInstanceOfType (typeof (InitializerVariableDeclaration), varst.Declarations[0].Declaration, "#8.3"); InitializerVariableDeclaration ini = (InitializerVariableDeclaration)varst.Declarations[0].Declaration; Assert.IsInstanceOfType (typeof (BinaryOperatorExpression), ini.Initializer, "#8.4"); Assert.AreEqual (Expression.Operation.Plus, ini.Initializer.Opcode, "#8.5"); Assert.IsTrue (parser.SyntaxOK ()); }
public void EmptyTest () { parser = new Parser (";".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.AreEqual (Statement.Operation.Empty, it.Element.Opcode, "#12.1"); Assert.IsTrue (parser.SyntaxOK ()); }
public void switchTest () { parser = new Parser ("switch (test) { case a: break; default: break;}".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (SwitchStatement), it.Element, "#9.1"); Assert.IsTrue (parser.SyntaxOK ()); }
public void ForTest () { parser = new Parser ("for (var i = 0 ; i < 5 ; i++ ) { break; }".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (DeclarationForStatement), it.Element, "#7.1"); DeclarationForStatement forst = ((DeclarationForStatement)it.Element); Assert.IsInstanceOfType (typeof (BlockStatement), forst.Body, "#7.2"); it = new DList<Statement, BlockStatement>.Iterator (((BlockStatement)forst.Body).Children); Assert.IsInstanceOfType (typeof (BreakOrContinueStatement), it.Element, "#7.3"); Assert.IsTrue (parser.SyntaxOK ()); }
private string PrintSyntax (string id, Parser parser) { string result = string.Empty; foreach (Diagnostic d in parser.Diagnostics) { result += (id + Enum.GetName (typeof (DiagnosticCode), d.Code)); } return result; }
public void Generatethis () { MJCP.Expression input = new MJCP.Expression (MJCP.Expression.Operation.@this, new TextSpan (0,0,0,0,0,0)); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.MethodCallExpression), exp, "#1"); //here strange so must detect that there is no parent function Assert.AreEqual ("GetGlobalObject", ((MSIA.MethodCallExpression)exp).Method.Name, "#2"); Assert.IsNull ( ((MSIA.MethodCallExpression)exp).Instance, "#3"); Assert.AreEqual (1,((MSIA.MethodCallExpression)exp).Arguments.Count, "#4"); Assert.IsInstanceOfType (typeof (MSIA.CodeContextExpression), ((MSIA.MethodCallExpression)exp).Arguments[0], "#5"); // 2cd pass with a parent function to have a real this Parser parser = new Parser ("function foo () { var bar; this.bar=5;}".ToCharArray ()); List<Comment> comms = new List<Comment> (); input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); exp = gen.Generate (input); IList<MSIA.Statement> statements = ((MSIA.BlockStatement)((MSIA.CodeBlockExpression)((MSIA.MethodCallExpression)((MSIA.BoundAssignment)exp).Value).Arguments[3]).Block.Body).Statements; exp = ((MSIA.ExpressionStatement)statements[0]).Expression;//the this Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), exp, "#6"); //((MSIA.BoundAssignment)exp).Reference.Name. //maptable. }
public void GeneratePrefixMinusMinus () { Parser parser = new Parser ("--foo;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), exp, "#1"); Assert.IsInstanceOfType (typeof (MSIA.ActionExpression), ((MSIA.BoundAssignment)exp).Value, "#2"); MSA.Action action = ((MSIA.ActionExpression)((MSIA.BoundAssignment)exp).Value).Action; Assert.IsInstanceOfType (typeof (MSA.DoOperationAction), action, "#3"); Assert.AreEqual (MSO.Subtract, ((MSA.DoOperationAction)action).Operation, "#4"); }
public void GenerateFunctionExp () { Parser parser = new Parser ("function foo() {}".ToCharArray (), idtable); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), exp, "#1"); Assert.AreEqual (MSO.None, ((MSIA.BoundAssignment)exp).Operator, "#2"); //TODO more test }
public void GeneratePercent () { Parser parser = new Parser ("foo % 1;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.ActionExpression), exp, "#1"); Assert.IsInstanceOfType (typeof (MSA.DoOperationAction), ((MSIA.ActionExpression)exp).Action, "#2"); Assert.AreEqual (MSO.Mod, ((MSA.DoOperationAction)((MSIA.ActionExpression)exp).Action).Operation, "#3"); }
public void GenerateIn () { Parser parser = new Parser ("foo in bar;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.MethodCallExpression), exp, "#1"); Assert.AreEqual (typeof (MJR.JSCompilerHelpers).GetMethod ("In"), ((MSIA.MethodCallExpression)exp).Method, "#2"); }
public void GenerateBarEqual () { Parser parser = new Parser ("foo |= 5;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), exp, "#1"); Assert.AreEqual (MSO.InPlaceBitwiseOr, ((MSIA.BoundAssignment)exp).Operator, "#2"); }
public void GenerateIdentifier () { Parser parser = new Parser ("foo;".ToCharArray (), idtable); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.BoundExpression), exp, "#1"); Assert.AreEqual (maptable.GetRowanID (idtable.InsertIdentifier ("foo")), ((MSIA.BoundExpression)exp).Name, "#2"); Assert.AreEqual (maptable.GetRowanID (idtable.InsertIdentifier("foo")), ((MSIA.BoundExpression)exp).Reference.Name, "#3"); }
public void DoWhileTest () { parser = new Parser ("do { }while (true);".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (DoStatement), it.Element, "#6.1"); DoStatement dost = ((DoStatement)it.Element); Assert.AreEqual (Expression.Operation.@true, dost.Condition.Opcode, "#6.2"); Assert.IsTrue (parser.SyntaxOK (), PrintSyntax ("#6", parser)); }
public void GenerateQuestion () { Parser parser = new Parser ("foo?1:0;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.ConditionalExpression), exp, "#1"); }
public void GeneratePostfixMinusMinus () { Parser parser = new Parser ("foo--;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.CommaExpression), exp, "#1"); Assert.AreEqual (2, ((MSIA.CommaExpression)exp).Expressions.Count, "#2"); Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), ((MSIA.CommaExpression)exp).Expressions[0], "#3"); Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), ((MSIA.CommaExpression)exp).Expressions[1], "#4"); Assert.IsInstanceOfType (typeof (MSIA.MethodCallExpression), ((MSIA.BoundAssignment)((MSIA.CommaExpression)exp).Expressions[0]).Value, "#5"); Assert.AreEqual (typeof (MJR.Convert).GetMethod ("ToNumber", new Type[] {typeof(object)}), ((MSIA.MethodCallExpression)((MSIA.BoundAssignment)((MSIA.CommaExpression)exp).Expressions[0]).Value).Method, "#6"); Assert.IsInstanceOfType (typeof (MSIA.ActionExpression), ((MSIA.BoundAssignment)((MSIA.CommaExpression)exp).Expressions[1]).Value, "#7"); MSIA.ActionExpression action = (MSIA.ActionExpression)((MSIA.BoundAssignment)((MSIA.CommaExpression)exp).Expressions[1]).Value; Assert.IsInstanceOfType (typeof (MSA.DoOperationAction), action.Action, "#8"); Assert.AreEqual (MSO.Subtract, ((MSA.DoOperationAction)action.Action).Operation, "#9"); }
public void IfTest () { parser = new Parser ("if (true) { } else ;".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (IfStatement), it.Element, "#4.1"); IfStatement ifelse = ((IfStatement)it.Element); Assert.AreEqual (Expression.Operation.@true, ifelse.Condition.Opcode, "#4.2"); Assert.IsTrue (parser.SyntaxOK ()); }
public void GenerateNull () { Parser parser = new Parser ("null;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Expression input = parser.ParseExpression (ref comms); gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Expression exp = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.ConstantExpression), exp, "#1"); Assert.IsNull (((MSIA.ConstantExpression)exp).Value, "#2"); }
public void WhileTest () { parser = new Parser ("while (true) { }".ToCharArray ()); List<Comment> comments = new List<Comment> (); DList<Statement, BlockStatement> list = parser.ParseProgram (ref comments); DList<Statement, BlockStatement>.Iterator it = new DList<Statement, BlockStatement>.Iterator (list); Assert.IsInstanceOfType (typeof (WhileStatement), it.Element, "#5.1"); WhileStatement whilest = ((WhileStatement)it.Element); Assert.AreEqual (Expression.Operation.@true, whilest.Condition.Opcode, "#5.2"); Assert.IsTrue (parser.SyntaxOK ()); }
public void GenerateVariableDeclaration () { Parser parser = new Parser ("var test = true;".ToCharArray ()); List<Comment> comms = new List<Comment> (); MJCP.Statement input = parser.ParseStatement (ref comms); //seems to be important to init the globals else it bug everywhere!! gen.SetGlobals (new MSIA.CodeBlock ("", new List<MSIA.Parameter> (0), new MSIA.EmptyStatement ())); MSIA.Statement result = gen.Generate (input); Assert.IsInstanceOfType (typeof (MSIA.ExpressionStatement), result, "#2"); MSIA.Expression exp = ((MSIA.ExpressionStatement)result).Expression; Assert.IsInstanceOfType (typeof (MSIA.CommaExpression), exp, "#3"); exp = ((MSIA.CommaExpression)exp).Expressions[0]; Assert.IsInstanceOfType (typeof (MSIA.BoundAssignment), exp, "#4"); //TODO test //((MSIA.BoundAssignment)exp).Reference; //((MSIA.BoundAssignment)exp).Value; //((MSIA.BoundAssignment)exp).Operator; }