public void GenerateCode(Writer.Writer Writer, API api)
 {
     foreach (var sect in this.switchSections)
     {
         sect.GenerateCode(Writer, api);
     }
 }
 public void GenerateCode(Writer.Writer Writer, API api)
 {
     if (statements != null)
     {
         statements.GenerateCode(Writer, api);
     }
 }
Example #3
0
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString($"\t\tfor ({identifier} of ");
     expression.GenerateCode(Writer, api);
     Writer.WriteStringLine(") {");
     this.body.GenerateCode(Writer, api);
     Writer.WriteStringLine("\t\t}");
 }
        public override void GenerateCode(Writer.Writer Writer, API api)
        {
            if (!Utils.passAssignExpression(token))
            {
                Writer.WriteString("(");
            }

            if (returnType is IntTypeNode)
            {
                Writer.WriteString("ToIntPrecision(");
            }

            if (leftComparativeType == "CharType" && rightComparativeType == "IntType")
            {
                Writer.WriteString("CharToInt(");
                leftOperand.GenerateCode(Writer, api);
                Writer.WriteString(")");
                Writer.WriteString($" {token.lexeme} ");
                Writer.WriteString("ToIntPrecision(");
                rightOperand.GenerateCode(Writer, api);
                Writer.WriteString(")");
            }
            else if (rightComparativeType == "CharType" && leftComparativeType == "IntType")
            {
                Writer.WriteString("ToIntPrecision(");
                leftOperand.GenerateCode(Writer, api);
                Writer.WriteString(")");
                Writer.WriteString($" {token.lexeme} ");
                Writer.WriteString("CharToInt(");
                rightOperand.GenerateCode(Writer, api);
                Writer.WriteString(")");
            }
            else if (leftComparativeType == "CharType" && rightComparativeType == "CharType")
            {
                Writer.WriteString("CharToInt(");
                leftOperand.GenerateCode(Writer, api);
                Writer.WriteString(")");
                Writer.WriteString($" {token.lexeme} ");
                Writer.WriteString("CharToInt(");
                rightOperand.GenerateCode(Writer, api);
                Writer.WriteString(")");
            }
            else
            {
                leftOperand.GenerateCode(Writer, api);
                Writer.WriteString($" {token.lexeme} ");
                rightOperand.GenerateCode(Writer, api);
            }

            if (returnType is IntTypeNode)
            {
                Writer.WriteString(")");
            }
            if (!Utils.passAssignExpression(token))
            {
                Writer.WriteString(")");
            }
        }
Example #5
0
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     primary.GenerateCode(Writer, api);
     if (nextExpression != null)
     {
         Writer.WriteString(".");
         nextExpression.GenerateCode(Writer, api);
     }
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     if (statements != null)
     {
         foreach (var stmt in statements)
         {
             stmt.GenerateCode(Writer, api);
         }
     }
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString($"\t\tswitch(");
     this.expression.GenerateCode(Writer, api);
     Writer.WriteString($") {{\n");
     if (this.switchBodyNode != null)
     {
         switchBodyNode.GenerateCode(Writer, api);
     }
     Writer.WriteString($"\t\t}}\n");
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString("\t\twhile (");
     this.expression.GenerateCode(Writer, api);
     Writer.WriteString(") {\n");
     if (this.body != null)
     {
         this.body.GenerateCode(Writer, api);
     }
     Writer.WriteString("\t\t}\n");
 }
Example #9
0
 public void GenerateCode(Writer.Writer Writer, API api)
 {
     if (this.caseType == TokenType.RW_CASE)
     {
         Writer.WriteString("\t\t\tcase ");
         this.expression.GenerateCode(Writer, api);
         Writer.WriteString(" :\n");
     }
     else if (caseType == TokenType.RW_DEFAULT)
     {
         Writer.WriteString("\t\t\tdefault:");
     }
 }
Example #10
0
 public void GenerateCode(Writer.Writer writer, API api)
 {
     foreach (var typeDef in typesDeclarations)
     {
         try{
             if (!typeDef.generated)
             {
                 typeDef.GenerateCode(writer, api);
             }
         }catch (NotImplementedException ex) {
             Console.WriteLine("TODO: [" + typeDef.GetType().Name + "](" + ex.Message + ") -> " + ex.StackTrace);
         }
     }
 }
Example #11
0
        public override void GenerateCode(Writer.Writer writer, API api)
        {
            var name = api.getNamespaceForType(this).Identifier.ToString() == "default" ? this.Identifier.ToString() : $"{api.getNamespaceForType(this).Identifier}.{this.Identifier}";

            writer.WriteStringLine($"GeneratedNamespace.{name} = {{");

            foreach (var _enum in this.EnumItems)
            {
                writer.WriteStringLine($"\t{_enum.Identifier} : {_enum.value},");
            }

            writer.WriteStringLine($"}}");
            this.generated = true;
        }
 public void GenerateCode(Writer.Writer Writer, API api)
 {
     if (this.switchLabels != null)
     {
         foreach (var slabel in this.switchLabels)
         {
             slabel.GenerateCode(Writer, api);
         }
         foreach (var stmt in this.stmts)
         {
             stmt.GenerateCode(Writer, api);
         }
     }
 }
 internal void GenerateCode(Writer.Writer writer, API api)
 {
     if (localVariables != null)
     {
         localVariables.GenerateCode(writer, api);
     }
     else if (localVariables != null)
     {
         foreach (var stmnt in statementExpresions)
         {
             stmnt.GenerateCode(writer, api);
         }
     }
 }
Example #14
0
        public override void GenerateCode(Writer.Writer Writer, API api)
        {
            foreach (var field in localVariables)
            {
                var _field = field.identifier.ToString();
                Writer.WriteString($"\t\tlet {_field}");

                if (field.assigner != null)
                {
                    Writer.WriteString(" = ");
                    field.assigner.GenerateCode(Writer, api);
                }

                Writer.WriteString(";\n");
            }
        }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString($"\t\tif( ");
     this.expression.GenerateCode(Writer, api);
     Writer.WriteString($") {{\n ");
     if (this.statements != null)
     {
         this.statements.GenerateCode(Writer, api);
     }
     Writer.WriteString($"\t\t}}\n\t\telse {{\n");
     if (this.elseBock != null)
     {
         elseBock.GenerateCode(Writer, api);
     }
     Writer.WriteString($"\t\t}}\n ");
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteStringLine($"\t\tfor (");
     if (this.Initializer != null)
     {
         this.Initializer.GenerateCode(Writer, api);
     }
     Writer.WriteString(";");
     if (this.expression != null)
     {
         expression.GenerateCode(Writer, api);
     }
     Writer.WriteString(";");
     Writer.WriteStringLine(") {");
     if (this.StatementBlock != null)
     {
         StatementBlock.GenerateCode(Writer, api);
     }
     Writer.WriteStringLine("\t\t}");
 }
Example #17
0
        public override void GenerateCode(Writer.Writer Writer, API api)
        {
            if (returnType == null)
            {
                Console.Write("");
            }
            string fullPath = "";

            if (isFirst)
            {
                fullPath = api.getFullNameByName(returnType);
            }

            if (isVariable && wasFoundStatic)
            {
                fullPath += ((this.parentName.Length > 0)?this.parentName + ".":"") + Name;
            }
            else if (isVariable && !wasFoundStatic)
            {
                fullPath = this.parentName + Name;
            }

            if (!isVariable)
            {
                if (fullPath.Length > 0)
                {
                    fullPath += (fullPath[fullPath.Length - 1] == '.')?Name:"." + Name;
                }
                else
                {
                    fullPath += "." + Name;
                }
            }
            // fullPath += Name;


            Writer.WriteString(fullPath);
        }
Example #18
0
        public CodeGenerator(List <string> paths)
        {
            this.paths = paths;
            this.trees = new Dictionary <string, CompilationUnitNode>();
            var lexer  = new Lexer(new InputString(Utils.txtIncludes), Resources.getTokenGenerators());
            var parser = new Parser(lexer);

            // trees.Add(parser.parse());
            trees["IncludesDefault"] = parser.parse();
            trees["IncludesDefault"].setOriginFile("IncludesDefault");


            string currentFile = "";

            var semantic = new Semantic(paths);

            try{
                var tempTrees = semantic.evaluate();
                foreach (var tree in tempTrees)
                {
                    trees[tree.Key] = tree.Value;
                }
                System.Console.Out.WriteLine("Success!");
            }catch (LexicalException ex) {
                System.Console.Out.WriteLine(ex.GetType().Name + " -> " + ex.Message);
            }catch (SyntaxTokenExpectedException ex) {
                System.Console.Out.WriteLine(ex.GetType().Name + " -> " + ex.Message);
            }catch (SemanticException ex) {
                System.Console.Out.WriteLine(ex.GetType().Name + " -> " + ex.Message);
            }
            this.api = new API(trees);
            // var _path = @"C:\Users\jobar\Documents\git\CStoJS_Compiler\GeneratedJs\generated.js";
            var _path = @"..\GeneratedJs\generated.js";

            writer = new Writer.Writer(_path);
        }
Example #19
0
        public void Writer_ConstructorWithParameters_ReturnsGivenValues(int id)
        {
            var result = new Writer.Writer(id);

            Assert.AreEqual(result.ID, id);
        }
Example #20
0
 public virtual void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString($"{token.lexeme}");
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString("\t\treturn");
     this.expression.GenerateCode(Writer, api);
     Writer.WriteString(";");
 }
Example #22
0
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString("continue");
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString("break");
 }
Example #24
0
 public virtual void GenerateCode(Writer.Writer Writer, API api)
 {
     Console.WriteLine($"Generating code for {this.Identifier}");
 }
Example #25
0
        public override void GenerateCode(Writer.Writer Writer, API api)
        {
            if (this.generated)
            {
                return;
            }
            var name = api.getNamespaceForType(this).Identifier.ToString() == "default" ? this.Identifier.ToString() : $"{api.getNamespaceForType(this).Identifier}.{this.Identifier}";

            var parentName = "GeneratedNamespace.Object";

            if (this.parents.Count != 0)
            {
                foreach (var parent in parents)
                {
                    if (parent.Value is ClassTypeNode)
                    {
                        parent.Value.GenerateCode(Writer, api);
                        parentName = api.getFullName(parent.Value) + "." + parent.Key;
                    }
                }
            }

            Writer.WriteStringLine($"GeneratedNamespace.{name} = class extends {parentName} {{");

            foreach (var constructor in Constructors)
            {
                var paramNames      = api.getParamNames(constructor.parameters);
                var constructorName = api.buildFixedParams(constructor.parameters);
                constructorName = constructorName.Replace("(", "");
                constructorName = constructorName.Replace(")", "");
                constructorName = constructorName.Replace(",", "_");
                var constName = constructorName == "" ? constructorName : "_" + constructorName;
                Writer.WriteString($"\t{this.Identifier}{constName}{paramNames} {{\n");
                constructor.statementBlock.GenerateCode(Writer, api);
                Writer.WriteString($"\t}}\n");
            }

            foreach (var method in Methods)
            {
                var paramNames = api.getParamNames(method.methodHeaderNode.fixedParams);
                var methodName = api.buildFixedParams(method.methodHeaderNode.fixedParams);
                methodName = methodName.Replace("(", "");
                methodName = methodName.Replace(")", "");
                methodName = methodName.Replace(",", "_");
                var methodHheaderName = methodName == "" ? methodName : "_" + methodName;
                Writer.WriteString($"\t{method.methodHeaderNode.Identifier}{methodHheaderName}{paramNames} {{\n");
                if (method.statemetBlock != null)
                {
                    method.statemetBlock.GenerateCode(Writer, api);
                }
                Writer.WriteString($"\t}}\n");
            }

            Writer.WriteString($"\tconstructor(){{\n");

            foreach (var field in Fields)
            {
                if (field.isStatic)
                {
                    continue;
                }
                var val = "null";
                Writer.WriteString($"\t\tthis.{field.identifier} = {val};\n");
            }
            var defaultArgs = @"
        let argumentos = Array.from(arguments);
        let argus = argumentos.slice(1);
        if (argumentos.length >= 1) this[arguments[0]](...argus);";

            Writer.WriteString(defaultArgs);

            Writer.WriteString($"\n\t}}\n");
            Writer.WriteString($"}}\n");

            foreach (var field in Fields)
            {
                if (!field.isStatic)
                {
                    continue;
                }
                var val = "null";
                Writer.WriteString($"GeneratedNamespace.{name}.{field.identifier} = {val};\n");
            }
            this.generated = true;
        }
Example #26
0
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     Writer.WriteString(token.lexeme);
 }
 public override void GenerateCode(Writer.Writer Writer, API api)
 {
     this.expressionNode.GenerateCode(Writer, api);
 }
Example #28
0
        public void Writer_EmptyConstructor_ReturnsDefaults()
        {
            var result = new Writer.Writer();

            Assert.AreEqual(result.ID, 0);
        }