public override IQueryElement VisitLiteral([NotNull] QueryGrammarParser.LiteralContext context) { if (context.NUMBER() != null) { IntegerLiteral literal = new IntegerLiteral(); literal.Value = context.NUMBER().GetText(); return(literal); } else if (context.STRING_VALUE() != null) { StringLiteral literal = new StringLiteral(); literal.Value = context.STRING_VALUE().GetText().Replace("'", "\""); return(literal); } else if (context.BOOL_VALUE() != null) { BoolLiteral literal = new BoolLiteral(); literal.Value = context.BOOL_VALUE().GetText(); return(literal); } else if (context.NULL_VALUE() != null) { NullLiteral literal = new NullLiteral(); return(literal); } throw new SyntaxException("Unsupported literal."); }
public IInterpreter IfBranches() { var interpreter = new Interpreter.Interpreter(); foreach (var mutability in Options.AllMutabilityModes) { var env = Language.Environment.Create(new Options() { AllowInvalidMainResult = true }.SetMutability(mutability)); var root_ns = env.Root; var main_func = root_ns.AddBuilder(FunctionBuilder.Create( "main", ExpressionReadMode.OptionalUse, NameFactory.Int64NameReference(), Block.CreateStatement(new IExpression[] { IfBranch.CreateIf(BoolLiteral.CreateFalse(), new[] { Return.Create(Int64Literal.Create("5")) }, IfBranch.CreateElse(new[] { Return.Create(Int64Literal.Create("2")) })) }))); ExecValue result = interpreter.TestRun(env); Assert.AreEqual(2L, result.RetValue.PlainValue); } return(interpreter); }
public IErrorReporter ErrorUnusedExpression() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; var system_ns = env.SystemNamespace; var var_decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(), Undef.Create()); var var_ref = NameReference.Create("x"); FunctionDefinition func_def = root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.OptionalUse, NameFactory.BoolNameReference(), Block.CreateStatement(new IExpression[] { var_decl, var_ref, Return.Create(BoolLiteral.CreateTrue()) }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.AreEqual(ErrorCode.ExpressionValueNotUsed, resolver.ErrorManager.Errors.Single().Code); Assert.AreEqual(var_ref, resolver.ErrorManager.Errors.Single().Node); } return(resolver); }
public IErrorReporter ReadingIfAsExpression() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { GlobalVariables = true, RelaxedMode = true }.SetMutability(mutability)); var root_ns = env.Root; var if_ctrl = IfBranch.CreateIf(BoolLiteral.CreateTrue(), new IExpression[] { Int64Literal.Create("5") }, IfBranch.CreateElse(new[] { Int64Literal.Create("7") })); root_ns.AddNode(VariableDeclaration.CreateStatement("x", NameFactory.Int64NameReference(), if_ctrl, EntityModifier.Public)); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return(resolver); }
public void parsesLiterals() { Parser p = new Parser(); Token a = new IntLiteral("0"); Token b = new StringLiteral(""); Token c = new BoolLiteral(""); Token s = new NonTerminal("s"); List <Token> production = new List <Token>(); production.Add(a); production.Add(b); production.Add(c); p.addProduction(s, production); p.setStartSymbol(s); p.prepareForParsing(); List <Token> parseThese = new List <Token>(); parseThese.Add(new IntLiteral("-50")); parseThese.Add(new StringLiteral("abc")); parseThese.Add(new BoolLiteral("true")); SyntaxTree parsed = p.parse(parseThese); Assert.AreEqual(new IntLiteral("-50"), parsed.root.children[0].token); Assert.AreNotEqual(new IntLiteral("50"), parsed.root.children[0].token); Assert.AreEqual(new StringLiteral("abc"), parsed.root.children[1].token); Assert.AreNotEqual(new StringLiteral("def"), parsed.root.children[1].token); Assert.AreEqual(new BoolLiteral("true"), parsed.root.children[2].token); Assert.AreNotEqual(new BoolLiteral("false"), parsed.root.children[2].token); }
public IErrorReporter ScopeShadowing() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { ScopeShadowing = true, DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create( "anything", null, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { VariableDeclaration.CreateStatement("x",null,Int64Literal.Create("2")), Block.CreateStatement(new IExpression[]{ // shadowing VariableDeclaration.CreateStatement("x", null, BoolLiteral.CreateFalse()), VariableDeclaration.CreateStatement("a",NameFactory.BoolNameReference(),NameReference.Create("x")), ExpressionFactory.Readout("a"), }), VariableDeclaration.CreateStatement("b",NameFactory.Int64NameReference(),NameReference.Create("x")), ExpressionFactory.Readout("b"), }))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); } return resolver; }
public static EocBoolLiteral Translate(CodeConverter C, BoolLiteral expr) { if (expr == null) { return(null); } return(new EocBoolLiteral(C, expr.Value)); }
private ExpressionNode ParseNonBindingExpression() { switch (Token) { case TokenType.LBRACK: return(ParseArrayLiteral()); case TokenType.TRUE_LITERAL: case TokenType.FALSE_LITERAL: { BoolLiteral ret = NewNode <BoolLiteral>(); ret.Value = TokenType.TRUE_LITERAL == Token ? true : false; NextToken(); return(ret); } case TokenType.STRING: return(ParseStringLiteral()); case TokenType.NUMBER: return(ParseNumberLiteral()); case TokenType.LBRACE: return(ParseObjectLiteral()); case TokenType.LPAREN: return(ParseParanthetical()); case TokenType.ADD: case TokenType.SUB: case TokenType.NOT: return(ParseUnaryExpression()); case TokenType.PERIOD: return(NewNode <ThisExpression>()); case TokenType.THIS: { ExpressionNode ret = NewNode <ThisExpression>(); NextToken(); return(ret); } case TokenType.NULL_LITERAL: { ExpressionNode ret = NewNode <NullLiteral>(); NextToken(); return(ret); } case TokenType.NEW: return(ParseNewExpression()); case TokenType.IDENTIFIER: return(ParseVariableExpression()); default: UnexpectedToken(); return(null); } }
protected override bool IsEqualTo(BoolLiteral right) { var rightBoolean = right as RoleBoolean; if (rightBoolean == null) { return false; } return m_metadataItem == rightBoolean.m_metadataItem; }
protected override bool IsEqualTo(BoolLiteral right) { var rightBoolean = right as CellIdBoolean; if (rightBoolean == null) { return false; } return m_index == rightBoolean.m_index; }
public bool Match(XMLLuaSearchBoolLiteral req, BoolLiteral real) { Logger.Debug($"bool_literal"); if (req.Value == real.Value) { SetSelectionIfSelected(real.Span, req); return(true); } return(false); }
public void TestBoolLiteral() { BoolLiteral node = new BoolLiteral(true, DefaultLineInfo); var trueNode = CheckSerializationRoundTrip(node); node = new BoolLiteral(false, DefaultLineInfo); var falseNode = CheckSerializationRoundTrip(node); Assert.NotEqual(trueNode.ToDebugString(), falseNode.ToDebugString()); }
public override bool Visit(BoolLiteral node) { if ((node.Value as IntegralValue).val != 0) { outputCode("true ", false, false); } else { outputCode("false ", false, false); } //Visit((OrdinalLiteral) node); return(true); }
public static Literal /*!*/ Create(Text.Span position, object value, AccessType access) { Literal result; if (value == null) { result = new NullLiteral(position); } else if (value.GetType() == typeof(int)) { result = new IntLiteral(position, (int)value); } else if (value.GetType() == typeof(string)) { result = new StringLiteral(position, (string)value); } else if (value.GetType() == typeof(bool)) { result = new BoolLiteral(position, (bool)value); } else if (value.GetType() == typeof(double)) { result = new DoubleLiteral(position, (double)value); } else if (value.GetType() == typeof(long)) { result = new LongIntLiteral(position, (long)value); } else if (value.GetType() == typeof(PhpBytes)) { result = new BinaryStringLiteral(position, ((PhpBytes)value).ReadonlyData); } else { throw new ArgumentException("value"); } // Debug.Assert(result != null); result.NodeCompiler <IExpressionCompiler>().Access = access; // return(result); }
/// <summary> /// Constructs the simple condition from the given list. /// </summary> /// <param name="conditionList">The condition list.</param> /// <returns></returns> private Expression constructSimpleCondition(List <Expression> conditionList) { Expression groupCondition; if (conditionList.Count > 0) { groupCondition = conditionList[0]; for (int index = 1; index < conditionList.Count; index++) { Position newPosition = mergePositions(groupCondition.Position, conditionList[index].Position); groupCondition = new BinaryEx(newPosition, Operations.And, groupCondition, conditionList[index]); } } else { groupCondition = new BoolLiteral(Position.Invalid, true); } return(groupCondition); }
public IErrorReporter ErrorCannotInferResultType() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true }.SetMutability(mutability)); var root_ns = env.Root; IExpression lambda = FunctionBuilder.CreateLambda(null, Block.CreateStatement(new IExpression[] { IfBranch.CreateIf(BoolLiteral.CreateFalse(), new[] { Return.Create(BoolLiteral.CreateTrue()) }), Return.Create(Int64Literal.Create("2")) })).Build(); root_ns.AddBuilder(FunctionBuilder.Create("me", ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement(new IExpression[] { // f = () => x VariableDeclaration.CreateStatement("f", null, lambda), ExpressionFactory.Readout("f") }))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotInferResultType, lambda)); } return(resolver); }
public IErrorReporter ErrorReadingFunctionVoidResult() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { GlobalVariables = true, RelaxedMode = true }.SetMutability(mutability)); var root_ns = env.Root; root_ns.AddBuilder(FunctionBuilder.Create( "foo", ExpressionReadMode.CannotBeRead, NameFactory.BoolNameReference(), Block.CreateStatement(new[] { Return.Create(BoolLiteral.CreateTrue()) })) .Parameters(FunctionParameter.Create("x", NameFactory.Int64NameReference(), usageMode: ExpressionReadMode.CannotBeRead))); root_ns.AddNode(VariableDeclaration.CreateStatement("i", NameFactory.Int64NameReference(), Undef.Create())); var call = FunctionCall.Create(NameReference.Create("foo"), FunctionArgument.Create(NameReference.Create("i"))); VariableDeclaration decl = VariableDeclaration.CreateStatement("x", NameFactory.BoolNameReference(), call, EntityModifier.Public); root_ns.AddNode(decl); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.CannotReadExpression, call)); } return(resolver); }
public override void VisitBoolLiteral(BoolLiteral x) { visitConstantNularyElement(x, (e) => e.BoolLiteral(x) ); }
private static Expression ParseExpression(BinaryReader reader, Encoding encoding, bool parseMember = true) { Expression result = null; byte type; while (true) { type = reader.ReadByte(); switch (type) { case 0x01: result = ParamListEnd.Instance; break; case 0x16: result = DefaultValueExpression.Instance; break; case 0x17: result = new NumberLiteral(reader.ReadDouble()); break; case 0x18: result = BoolLiteral.ValueOf(reader.ReadInt16() != 0); break; case 0x19: result = new DateTimeLiteral(DateTime.FromOADate(reader.ReadDouble())); break; case 0x1A: result = new StringLiteral(reader.ReadBStr(encoding)); break; case 0x1B: result = new ConstantExpression(reader.ReadInt32()); break; case 0x1C: result = new ConstantExpression((short)(reader.ReadInt16() - 1), (short)(reader.ReadInt16() - 1)); break; case 0x1D: // 0x1D 0x38 <Int32:VarId> continue; case 0x1E: result = new MethodPtrExpression(reader.ReadInt32()); break; case 0x21: result = ParseCallExpressionWithoutType(reader, encoding); break; case 0x23: result = new EmnuConstantExpression((short)(reader.ReadInt16() - 1), (short)(reader.ReadInt16() - 1), reader.ReadInt32() - 1); break; case 0x37: continue; case 0x1F: { var array = new ArrayLiteralExpression(); Expression exp; while (!((exp = ParseExpression(reader, encoding)) is ArrayLiteralEnd)) { array.Add(exp); } result = array; } break; case 0x20: result = ArrayLiteralEnd.Instance; break; case 0x38: // ThisCall Or 访问变量 { int variable = reader.ReadInt32(); if (variable == 0x0500FFFE) { reader.ReadByte(); // 0x3A return(ParseExpression(reader, encoding, true)); } else { result = new VariableExpression(variable); parseMember = true; } } break; case 0x3B: result = new NumberLiteral(reader.ReadInt32()); break; default: throw new Exception($"Unknown Type: {type.ToString("X2")}"); } break; } if (parseMember && (result is VariableExpression || result is CallExpression || result is AccessMemberExpression || result is AccessArrayExpression)) { while (reader.BaseStream.Position != reader.BaseStream.Length) { switch (reader.ReadByte()) { case 0x39: int memberId = reader.ReadInt32(); if (EplSystemId.GetType(memberId) == EplSystemId.Type_StructMember) { result = new AccessMemberExpression(result, reader.ReadInt32(), memberId); } else { result = new AccessMemberExpression(result, (short)(reader.ReadInt16() - 1), (short)(reader.ReadInt16() - 1), memberId - 1); } break; case 0x3A: result = new AccessArrayExpression(result, ParseExpression(reader, encoding, false)); break; case 0x37: goto parse_member_finish; default: reader.BaseStream.Position -= 1; goto parse_member_finish; } } } parse_member_finish: return(result); }
public virtual T Visit(BoolLiteral node) { return(Visit((OrdinalLiteral)node)); }
public void Visit(BoolLiteral bl) { _sb.Append(bl.Value); }
/// <summary> /// See <see cref="BoolLiteral.IsIdentifierEqualTo"/>. Member restriction can be incomplete for this operation. /// </summary> protected override bool IsIdentifierEqualTo(BoolLiteral right) { MemberRestriction rightOneOfConst = right as MemberRestriction; if (rightOneOfConst == null) { return false; } if (object.ReferenceEquals(this, rightOneOfConst)) { return true; } return MemberProjectedSlot.EqualityComparer.Equals(m_restrictedMemberSlot, rightOneOfConst.m_restrictedMemberSlot); }
public override bool Visit(BoolLiteral node) { Visit((OrdinalLiteral)node); return(true); }
override public void VisitBoolLiteral(BoolLiteral x) { _serializer.Serialize(typeof(BoolLiteral).Name, SerializeSpan(x.Span), new NodeObj("Value", x.Value.ToString())); }
public void visit(BoolLiteral n) { throw new NotImplementedException(); }
public void Visit(BoolLiteral bl) { // Nothing to do here... }
public ASTType visit(BoolLiteral n) { return new BoolType(); }
public void Visit(BoolLiteral node) { VisitLuaFunction(node); }
virtual public void VisitBoolLiteral(BoolLiteral x) { // nothing }
public IErrorReporter TranslationTableOfInferredCommonTypes() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { }.SetMutability(mutability)); var root_ns = env.Root; TemplateParameter template_param = TemplateParametersBuffer.Create("T").Values.Single(); root_ns.AddBuilder(FunctionBuilder.Create("getMe", new[] { template_param }, ExpressionReadMode.OptionalUse, NameFactory.UnitNameReference(), Block.CreateStatement()) .Parameters(FunctionParameter.Create("a", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead), FunctionParameter.Create("b", NameFactory.ReferenceNameReference("T"), ExpressionReadMode.CannotBeRead))); FunctionCall call = FunctionCall.Create(NameReference.Create("getMe"), NameReference.Create("x"), NameReference.Create("y")); root_ns.AddBuilder(FunctionBuilder.Create("common", NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("x", NameFactory.ReferenceNameReference(NameFactory.Int64NameReference()), Int64Literal.Create("3")), VariableDeclaration.CreateStatement("y", NameFactory.ReferenceNameReference(NameFactory.BoolNameReference()), BoolLiteral.CreateTrue()), call ))); resolver = NameResolver.Create(env); Assert.AreEqual(0, resolver.ErrorManager.Errors.Count); // the actual point of this test are those two lines checking if we get correct translation table for entire // instance of the called function Assert.IsTrue(call.Resolution.TargetFunctionInstance.Translation.Translate(template_param, out IEntityInstance common_instance)); Assert.AreEqual(resolver.Context.Env.IEquatableType.InstanceOf, common_instance); } return(resolver); }
public virtual void Visit(BoolLiteral literal) { }
public void Visit(BoolLiteral bl) { var opCode = bl.Value ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0; _instructions.Add(Instruction.Create(opCode)); }
/// <summary> /// See <see cref="BoolLiteral.IsEqualTo"/>. Member restriction can be incomplete for this operation. /// </summary> protected override bool IsEqualTo(BoolLiteral right) { MemberRestriction rightRestriction= right as MemberRestriction; if (rightRestriction == null) { return false; } if (object.ReferenceEquals(this, rightRestriction)) { return true; } if (false == MemberProjectedSlot.EqualityComparer.Equals(m_restrictedMemberSlot, rightRestriction.m_restrictedMemberSlot)) { return false; } return m_domain.IsEqualTo(rightRestriction.m_domain); }
public override void visit(BoolLiteral n) { n.Scope = Scope; }
public void Visit(BoolLiteral bl) { _result = bl.Type = _boolType; }
public IErrorReporter ErrorIfScope() { NameResolver resolver = null; foreach (var mutability in Options.AllMutabilityModes) { var env = Environment.Create(new Options() { DiscardingAnyExpressionDuringTests = true, }.SetMutability(mutability)); var root_ns = env.Root; NameReference bad_ref = NameReference.Create("x"); root_ns.AddBuilder(FunctionBuilder.Create( "testing", NameFactory.UnitNameReference(), Block.CreateStatement( VariableDeclaration.CreateStatement("b", NameFactory.BoolNameReference(), Undef.Create(), env.Options.ReassignableModifier()), IfBranch.CreateIf(VariableDeclaration.CreateExpression("x", null, BoolLiteral.CreateTrue()), // x is in scope Assignment.CreateStatement("b", "x"), IfBranch.CreateElse( // x is in scope as well Assignment.CreateStatement("b", "x"))), // here x is not in the scope (is already removed) Assignment.CreateStatement(NameReference.Create("b"), bad_ref), ExpressionFactory.Readout("b") ))); resolver = NameResolver.Create(env); Assert.AreEqual(1, resolver.ErrorManager.Errors.Count); Assert.IsTrue(resolver.ErrorManager.HasError(ErrorCode.ReferenceNotFound, bad_ref)); } return(resolver); }
// Literals public override void Visit(BoolLiteral literal) { Write(literal.Value.ToString()); }
public override void VisitBoolLiteral(BoolLiteral x) { _composer.ConsumeLiteral(x); }
public static TypeBuilder WithEquatableEquals(this TypeBuilder builder, EntityModifier modifier = null) { return(builder.With(FunctionBuilder.Create(NameFactory.EqualOperator, ExpressionReadMode.ReadRequired, NameFactory.BoolNameReference(), Block.CreateStatement( IfBranch.CreateIf(IsSame.Create(NameReference.CreateThised(), NameReference.Create("cmp")), new[] { Return.Create(BoolLiteral.CreateTrue()) }), // let obj = cmp cast? Self VariableDeclaration.CreateStatement("obj", null, ExpressionFactory.CheckedSelfCast("cmp", NameFactory.ReferenceNameReference(builder.CreateTypeNameReference(TypeMutability.ReadOnly)))), // return this==obj.value Return.Create(ExpressionFactory.IsEqual(NameReference.Create(NameFactory.ThisVariableName), NameReference.Create("obj"))))) .SetModifier(EntityModifier.Override | modifier) .Parameters(FunctionParameter.Create("cmp", NameFactory.ReferenceNameReference(NameFactory.IEquatableNameReference(TypeMutability.ReadOnly)))))); }