public void NamedExpression() { INamedExpression expression = new DoubleColonExpression(new ConstantExpression(1), "name"); Assert.AreEqual(new ConstantExpression(1), expression.TargetExpression); Assert.AreEqual("name", expression.Name); }
public void ParseDoubleColon() { Parser parser = new Parser("Module1::PI"); var expected = new DoubleColonExpression(new NameExpression("Module1"), "PI"); var result = parser.ParseExpression(); Assert.IsNotNull(result); Assert.AreEqual(expected, result); Assert.IsNull(parser.ParseCommand()); }
public void EvaluateTypeConstant() { Machine machine = new Machine(); DoubleColonExpression expression = new DoubleColonExpression(new ConstantExpression(typeof(TokenType)), "Name"); var result = expression.Evaluate(machine.RootContext); Assert.IsNotNull(result); Assert.AreEqual(TokenType.Name, result); }
public void EvaluateConstant() { Machine machine = new Machine(); machine.ExecuteText("module MyModule; FOO = 3; end"); DoubleColonExpression expression = new DoubleColonExpression(new NameExpression("MyModule"), "FOO"); var result = expression.Evaluate(machine.RootContext); Assert.IsNotNull(result); Assert.AreEqual(3, result); }
public void Equals() { DoubleColonExpression expr1 = new DoubleColonExpression(new ConstantExpression(1), "foo"); DoubleColonExpression expr2 = new DoubleColonExpression(new ConstantExpression(2), "foo"); DoubleColonExpression expr3 = new DoubleColonExpression(new ConstantExpression(1), "bar"); DoubleColonExpression expr4 = new DoubleColonExpression(new ConstantExpression(1), "foo"); Assert.IsTrue(expr1.Equals(expr4)); Assert.IsTrue(expr4.Equals(expr1)); Assert.AreEqual(expr1.GetHashCode(), expr4.GetHashCode()); Assert.IsFalse(expr1.Equals(expr2)); Assert.IsFalse(expr2.Equals(expr1)); Assert.IsFalse(expr1.Equals(expr3)); Assert.IsFalse(expr3.Equals(expr1)); Assert.IsFalse(expr1.Equals(null)); Assert.IsFalse(expr1.Equals("foo")); }
public void EvaluateUndefinedConstant() { Machine machine = new Machine(); machine.ExecuteText("module MyModule;end"); DoubleColonExpression expression = new DoubleColonExpression(new NameExpression("MyModule"), "FOO"); try { expression.Evaluate(machine.RootContext); Assert.Fail(); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NameError)); Assert.AreEqual("unitialized constant MyModule::FOO", ex.Message); } }
private IExpression ParseDefExpression() { string name = this.ParseName(); INamedExpression named = null; if (name == "self") { named = new SelfExpression(); } else { named = new NameExpression(name); } while (true) { if (this.TryParseToken(TokenType.Separator, ".")) { string newname = this.ParseName(); named = new DotExpression(named, newname, new IExpression[0]); continue; } if (this.TryParseToken(TokenType.Separator, "::")) { string newname = this.ParseName(); named = new DoubleColonExpression(named, newname); continue; } break; } IList <string> parameters = this.ParseParameterList(); IExpression body = this.ParseCommandList(); return(new DefExpression(named, parameters, body)); }
private ClassExpression ParseClassExpression() { string name = this.ParseName(); INamedExpression named = null; if (name == "self") { named = new SelfExpression(); } else { named = new NameExpression(name); } while (true) { if (this.TryParseToken(TokenType.Separator, "::")) { string newname = this.ParseName(); named = new DoubleColonExpression(named, newname); continue; } break; } this.ParseEndOfCommand(); IExpression body = this.ParseCommandList(); if (!Predicates.IsConstantName(named.Name)) { throw new SyntaxError("class/module name must be a CONSTANT"); } return(new ClassExpression(named, body)); }
private IExpression ParseTerm() { IExpression expression = null; if (this.TryParseToken(TokenType.Operator, "-")) { expression = new NegativeExpression(this.ParseTerm()); } else if (this.TryParseToken(TokenType.Operator, "+")) { expression = this.ParseTerm(); } else if (this.TryParseToken(TokenType.Operator, "!")) { expression = new NegationExpression(this.ParseTerm()); } else { expression = this.ParseSimpleTerm(); } if (expression == null) { return(null); } while (true) { if (this.TryParseToken(TokenType.Separator, ".")) { string name = this.ParseName(); if (this.TryParseToken(TokenType.Separator, "{")) { expression = new DotExpression(expression, name, new IExpression[] { this.ParseBlockExpression(true) }); } else if (this.NextTokenStartsExpressionList()) { expression = new DotExpression(expression, name, this.ParseExpressionList()); } else { expression = new DotExpression(expression, name, new IExpression[0]); } continue; } if (this.TryParseToken(TokenType.Separator, "::")) { string name = this.ParseName(); expression = new DoubleColonExpression(expression, name); continue; } if (this.TryParseToken(TokenType.Separator, "[")) { IExpression indexexpr = this.ParseExpression(); this.ParseToken(TokenType.Separator, "]"); expression = new IndexedExpression(expression, indexexpr); continue; } break; } return(expression); }
public void GetLocalVariables() { DoubleColonExpression expression = new DoubleColonExpression(new ConstantExpression(typeof(TokenType)), "Name"); Assert.IsNull(expression.GetLocalVariables()); }