Esempio n. 1
0
        public void visit_VarDeclaration(AST node)
        {
            BuiltType bType = node.type;
            string    type  = "";

            if (bType == BuiltType.BOOLEAN)
            {
                type = "bool";
            }
            else if (bType == BuiltType.INTEGER)
            {
                type = "int";
            }
            else if (bType == BuiltType.REAL)
            {
                type = "double";
            }
            else if (bType == BuiltType.STRING)
            {
                type = "char";
            }
            for (int i = 0; i < node.nodes.Count() - 1; i++)
            {
                if (bType == BuiltType.STRING)
                {
                    this.generatedCode.AppendLine(this.indent(stm.currentTable.scopeLevel) + type + " " + node.nodes[i].token.lexeme + "[255]" + ";");
                }
                else
                {
                    this.generatedCode.AppendLine(this.indent(stm.currentTable.scopeLevel) + type + " " + node.nodes[i].token.lexeme + ";");
                }
            }
        }
Esempio n. 2
0
        public void visit_Write(AST node)
        {
            string indent    = this.indent(this.stm.currentTable.scopeLevel);
            string statement = indent + "printf(\"";
            AST    args      = node.nodes[0];

            foreach (AST arg in args.nodes)
            {
                BuiltType type = arg.type;
                if (type == BuiltType.INTEGER)
                {
                    statement += "%d";
                }
                else if (type == BuiltType.REAL)
                {
                    statement += "%e";
                }
                else if (type == BuiltType.STRING)
                {
                    statement += "%s";
                }
            }
            statement += "\"";
            foreach (AST arg in args.nodes)
            {
                statement += ",";
                statement += (string)this.visit(arg);
            }
            statement += ");";
            this.generatedCode.AppendLine(statement);
        }
Esempio n. 3
0
        public string Invocation()
        {
            var invocation = $"new {BuiltType.FullNameInCode()}({Parameters.Select(x => x.Usage).Join(", ")})";

            if (Setters.Any())
            {
                invocation += $"{{{Setters.Select(x => x.Assignment()).Join(", ")}}}";
            }

            return(invocation);
        }
Esempio n. 4
0
        public void WriteExpressions(LambdaDefinition definition)
        {
            // No next, not disposable

            var isDisposed = BuiltType.CanBeCastTo <IDisposable>() ||
                             BuiltType.CanBeCastTo <IAsyncDisposable>();

            var callCtor = Expression.New(Ctor, Parameters.Select(definition.ExpressionFor));

            if (Next == null && !isDisposed && !Setters.Any())
            {
                definition.Body.Add(callCtor);
            }
            else
            {
                var variableExpr = Expression.Parameter(BuiltType, Variable.Usage);
                definition.RegisterExpression(Variable, variableExpr);
                definition.Assign(variableExpr, callCtor);

                foreach (var setter in Setters)
                {
                    var setMethod = BuiltType.GetProperty(setter.PropertyName).SetMethod;

                    var value = definition.ExpressionFor(setter.Variable);
                    var call  = Expression.Call(variableExpr, setMethod, value);
                    definition.Body.Add(call);
                }

                if (isDisposed)
                {
                    definition.RegisterDisposable(variableExpr, Variable.VariableType);
                }

                if (Next == null)
                {
                    definition.Body.Add(definition.ExpressionFor(Variable));
                }
                else
                {
                    if (Next is IResolverFrame next)
                    {
                        next.WriteExpressions(definition);
                    }
                    else
                    {
                        throw new InvalidCastException($"{Next.GetType().GetFullName()} does not implement {nameof(IResolverFrame)}");
                    }
                }
            }
        }
Esempio n. 5
0
        public void visit_Function(AST node)
        {
            string    name = node.nodes[0].token.lexeme;
            BuiltType type = this.currentTable.lookType(node.nodes[2].token.lexeme);

            if (this.currentTable.lookup(name) == null)
            {
                this.currentTable.define(new FunctionSymbol(name, type, Category.FUNCTION, this.parameterListGenerator(node)));
            }
            else
            {
                this.ThrowErrorMessage(new DuplicateDeclarationError(node.nodes[0].token));
            }
            //parameters here
            this.enterNewScope(name, this.currentTable.scopeLevel, node.nodeID);
            this.visit(node.nodes[3]);
            this.exitCurrentScope();
        }
Esempio n. 6
0
        public void visit_VarDeclaration(AST node)
        {
            List <AST> nodes = node.nodes;
            BuiltType  type  = this.currentTable.lookType(nodes.Last().token.lexeme);

            for (int i = 0; i < nodes.Count - 1; i++)
            {
                string name = nodes[i].token.lexeme;
                //check if current scope has duplicates or if enclosing function/procedure has clashing parameters
                if (this.currentTable.lookupOnlyThisScope(name) == null && this.currentTable.lookupInEnclosingProcedureOrFunction(name) == null)
                {
                    this.currentTable.define(new Symbol(name, type, Category.VARIABLE));
                }
                else
                {
                    this.ThrowErrorMessage(new DuplicateDeclarationError(nodes[i].token));
                }
            }
        }
Esempio n. 7
0
        public void visit_Read(AST node)
        {
            string statement = "";
            string indent    = this.indent(stm.currentTable.scopeLevel);

            statement += indent;
            statement += "scanf(\"";
            foreach (AST n in node.nodes)
            {
                BuiltType type = n.type;
                if (type == BuiltType.INTEGER)
                {
                    statement += "%d";
                }
                else if (type == BuiltType.REAL)
                {
                    statement += "%e";
                }
                else if (type == BuiltType.STRING)
                {
                    statement += "%s";
                }
            }
            statement += "\"";
            foreach (AST n in node.nodes)
            {
                string    id   = n.nodes[0].token.lexeme;
                BuiltType type = n.type;
                statement += ",";
                if (n.type == BuiltType.STRING)
                {
                    statement += id;
                }
                else
                {
                    statement += "&" + id;
                }
            }
            statement += ");";
            this.generatedCode.AppendLine(statement);
        }
Esempio n. 8
0
        public void visit_Function(AST node)
        {
            AST       id         = node.nodes[0];
            AST       parameters = node.nodes[1];
            BuiltType bType      = this.stm.currentTable.lookup(id.token.lexeme).type;
            AST       block      = node.nodes[3];
            string    type       = "";

            if (bType == BuiltType.INTEGER)
            {
                type = "int";
            }
            else if (bType == BuiltType.BOOLEAN)
            {
                type = "bool";
            }
            else if (bType == BuiltType.REAL)
            {
                type = "double";
            }
            else if (bType == BuiltType.STRING)
            {
                type = "char";
            }
            string function = type + " " + id.token.lexeme + "(";

            for (int i = 0; i < parameters.nodes.Count; i++)
            {
                function += this.visit(parameters.nodes[i]);
                if (i != parameters.nodes.Count - 1)
                {
                    function += ", ";
                }
            }
            function += ")";
            this.generatedCode.AppendLine(function);
            this.stm.enterScope(id.token.lexeme, stm.currentTable.scopeLevel, node.nodeID);
            this.visit(node.nodes[3]);
            this.stm.exitScope();
        }
Esempio n. 9
0
        public string visit_Reference(AST node)
        {
            BuiltType bType = node.type;
            string    type  = "";

            if (bType == BuiltType.INTEGER)
            {
                type = "int*";
            }
            else if (bType == BuiltType.BOOLEAN)
            {
                type = "bool*";
            }
            else if (bType == BuiltType.REAL)
            {
                type = "double*";
            }
            else if (bType == BuiltType.STRING)
            {
                type = "char*";
            }
            return(type + " " + node.nodes[0].token.lexeme);
        }
Esempio n. 10
0
        private List <Symbol> parameterListGenerator(AST node)
        {
            List <Symbol> paramList  = new List <Symbol>();
            AST           parameters = node.nodes[1];

            if (parameters != null)
            {
                foreach (AST n in parameters.nodes)
                {
                    string    parName = n.nodes[0].token.lexeme;
                    BuiltType parType = this.currentTable.lookType(n.nodes[1].token.lexeme);
                    if (n is Parameter)
                    {
                        paramList.Add(new Symbol(parName, parType, Category.PARAMETER));
                    }
                    else if (n is Reference)
                    {
                        paramList.Add(new Symbol(parName, parType, Category.REFERENCE));
                    }
                }
            }
            return(paramList);
        }
Esempio n. 11
0
 public StatementTypeError(AST statement, BuiltType found, BuiltType expected)
 {
     this.statement = statement;
     this.found     = found;
     this.expected  = expected;
 }
Esempio n. 12
0
 public FunctionSymbol(string var, BuiltType type, Category category, List <Symbol> parameters) : base(var, type, category)
 {
     this.parameters = parameters;
 }
Esempio n. 13
0
 public Symbol(string var, BuiltType type, Category category)
 {
     this.var      = var;
     this.type     = type;
     this.category = category;
 }