Example #1
0
 public ThreeAddressCode(MethodDefinition method, Triplet root, List <Triplet> rawTriplets, List <VariableDefinition> temporaryVariables)
 {
     this.method             = method;
     this.root               = root;
     this.rawTriplets        = rawTriplets;
     this.temporaryVariables = temporaryVariables;
 }
Example #2
0
        /// <summary>
        /// return ident | digit
        /// </summary>
        /// <returns><c>true</c> if this instance is return triplet the specified triplet; otherwise, <c>false</c>.</returns>
        /// <param name="triplet">Triplet.</param>
        bool IsReturnTriplet(out Triplet triplet)
        {
            triplet = new Triplet(TripletOpCode.Return);

            consumeToken();
            // Support only return ident
            if (Tok().kind == Token.TokenKind.Ident)
            {
                // FIXME: Here we should keep a list of active variables and the we should use it,
                // because this is not a variable definition but a variable reference.
                triplet.Operand1 = new VariableDefinition(Tok().data, /*TypeReference*/ null);
                return(true);
            }
            return(false);
        }
Example #3
0
 /// <summary>
 /// result = operand1 [operator] [operand2]
 /// </summary>
 /// <returns><c>true</c> if this instance is assignment triplet the specified triplet; otherwise, <c>false</c>.</returns>
 /// <param name="triplet">Triplet.</param>
 bool IsAssignmentTriplet(out Triplet triplet)
 {
     triplet        = new Triplet(TripletOpCode.Assignment);
     triplet.Result = new VariableDefinition(Tok().data, /*TypeReference*/ null);
     consumeToken();
     // consume the =
     consumeToken();
     if (Tok().kind != Token.TokenKind.Ident && Tok().kind != Token.TokenKind.Digit)
     {
         errors.AppendLine("identifier or digit expected");
         return(false);
     }
     if (Tok().kind == Token.TokenKind.Ident)
     {
         triplet.Operand1 = new VariableDefinition(Tok().data, /*TypeReference*/ null);
     }
     else if (Tok().kind == Token.TokenKind.Digit)
     {
         triplet.Operand1 = Int64.Parse(Tok().data);
     }
     return(true);
 }
Example #4
0
        /// <summary>
        /// The triplets have the form
        /// triplet := [label] ident operator ident [operator] [ident]
        /// </summary>
        /// <returns><c>true</c> if this instance is triplet; otherwise, <c>false</c>.</returns>
        bool IsTriplet(out Triplet triplet)
        {
            triplet = null;
            if (Tok().kind != Token.TokenKind.Ident)
            {
                errors.AppendLine("Triplet must begin with an ident.");
                return(false);
            }

            Token nextTok = lookAhead();

            //FIXME: Skip the label for now: L1:
            if (nextTok.kind == Token.TokenKind.Colon)
            {
                consumeToken();
                consumeToken();
                nextTok = lookAhead();
            }

            if (Tok().data == "return")
            {
                IsReturnTriplet(out triplet);
            }
            else if (nextTok.kind == Token.TokenKind.Equals)
            {
                IsAssignmentTriplet(out triplet);
            }

            consumeToken();
            if (Tok().kind == Token.TokenKind.Semicolon)
            {
                errors.AppendLine("; expected");
                return(false);
            }
            return(true);
        }