示例#1
0
        private void CompileFormalList(Node ActiveNode)
        {
            if (ActiveNode.Nodes[0].TypeToken != TokenType.Epsilon)
            {
                FormalTypes.Add(ActiveNode.Nodes[0]);

                FormalIds.Add(ActiveNode.Nodes[1]);

                ParametersStack[ParametersStack.Count - 1].Add(ActiveNode.Nodes[1].Value);

                CreateCode(ActiveNode.Nodes[2]);
            }
        }
示例#2
0
        private Collections.List <PropertyDeclaration> VisitProperties(List <Property> properties)
        {
            var props = new Collections.List <PropertyDeclaration>(properties.Count);

            foreach (Property prop in properties)
            {
                props.Add(VisitClassProperty(prop));
            }

            return(props);
        }
示例#3
0
        private Collections.List <PropertyDeclaration> VisitClassBody(ClassBody classBody)
        {
            var properties = new Collections.List <PropertyDeclaration>(classBody.Body.Count);

            foreach (ClassProperty classProperty in classBody.Body)
            {
                properties.Add(VisitClassProperty(classProperty));
            }

            return(properties);
        }
        private UstSpecific.ArrayPatternExpression VisitArrayPattern(ArrayPattern arrayPattern)
        {
            var elements = new Collections.List <ParameterDeclaration>(arrayPattern.Elements.Count);

            foreach (IArrayPatternElement elem in arrayPattern.Elements)
            {
                ParameterDeclaration paramDecl = VisitArrayPatternElement(elem);
                elements.Add(paramDecl);
            }

            return(new UstSpecific.ArrayPatternExpression(elements, GetTextSpan(arrayPattern)));
        }
示例#5
0
        private Collections.List <ParameterDeclaration> VisitParameters(Esprima.Ast.List <INode> parameters)
        {
            var result = new Collections.List <ParameterDeclaration>(parameters.Count);

            foreach (INode param in parameters)
            {
                UstTokens.IdToken name;
                if (param is Identifier identifier)
                {
                    name = VisitIdentifier(identifier);
                }
                else
                {
                    // TODO: extend parameter declaration
                    name = new UstTokens.IdToken("", GetTextSpan(param));
                }
                result.Add(new ParameterDeclaration(null, null, name, name.TextSpan));
            }

            return(result);
        }
        public RootUst Convert(ParseTree langParseTree)
        {
            try
            {
                var esprimaParseTree = (JavaScriptEsprimaParseTree)langParseTree;
                if (SourceFile == null)
                {
                    SourceFile = esprimaParseTree.SourceFile;
                }
                var program = VisitProgram(esprimaParseTree.SyntaxTree);

                var rootUst = new RootUst(SourceFile, Language.JavaScript, GetTextSpan(esprimaParseTree.SyntaxTree))
                {
                    Nodes = new Ust[] { program },
                };

                var comments = new Collections.List <CommentLiteral>(esprimaParseTree.Comments.Count);
                foreach (Comment comment in esprimaParseTree.Comments)
                {
                    TextSpan textSpan = GetTextSpan(comment);
                    comments.Add(new CommentLiteral(SourceFile.GetSubstring(textSpan), textSpan)
                    {
                        Root = rootUst,
                    });
                }

                rootUst.Comments = comments.ToArray();
                rootUst.Root     = ParentRoot;
                rootUst.FillAscendants();

                return(rootUst);
            }
            catch (Exception ex) when(!(ex is ThreadAbortException))
            {
                Logger.LogError(new ConversionException(SourceFile, ex));
                return(null);
            }
        }
示例#7
0
        private TypeDeclaration VisitClassDeclaration(ClassDeclaration classDeclaration)
        {
            var name = classDeclaration.Id != null
                ? VisitIdentifier(classDeclaration.Id)
                : null;

            var baseTypes = new Collections.List <TypeToken>();

            if (classDeclaration.SuperClass != null)
            {
                if (VisitExpression(classDeclaration.SuperClass) is TypeToken superClassTypeToken)
                {
                    baseTypes.Add(superClassTypeToken);
                }
            }

            var properties = VisitClassBody(classDeclaration.Body);

            return(new TypeDeclaration(null, name, properties, GetTextSpan(classDeclaration))
            {
                BaseTypes = baseTypes
            });
        }
示例#8
0
        private static void Translate <TEntity>(System.Data.Common.DbDataReader reader, out Collections.List <TEntity> entities) where TEntity : class
        {
            // Initilize list
            entities = new Collections.List <TEntity>();

            System.Type type = typeof(TEntity);
            System.Reflection.MethodInfo methodInfo = type.GetMethod("Factory", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

            while (reader.Read())
            {
                // Create Object
                TEntity entity = (TEntity)methodInfo.Invoke(null, new object[0]);

                foreach (System.Reflection.PropertyInfo property in type.GetProperties())
                {
                    if (IsComplexType(property))        // Complex Type
                    {
                        Entities.EntityObject complexEntity = null;
                        if (TryGetValue(property, reader, out complexEntity))
                        {
                            property.SetValue(entity, complexEntity, null);
                        }
                    }
                    else if (IsPrimitiveType(property)) // Primitive Type
                    {
                        object value = null;
                        if (TryGetValue(property.Name, reader, out value))
                        {
                            property.SetValue(entity, value, null);
                        }
                    }
                }

                entities.Add(entity);
            }
        }
示例#9
0
        /// <summary>
        /// Private function for handling subroutines.
        /// Encountered subroutines are handled first! Nested subs may not yield the expected outcome... Refer to sample script file.
        /// </summary>
        /// <param name="t_arg">Token that holds the ParL value after the function name</param>
        /// <returns>Token after the one that contains the ParR value</returns>
        private Token ParseFunction(ref Token t_arg)
        {
            string fname = t_arg.val;

            if (t_arg.next.kind != 4)
            {
                throw new Exception("Expected '(' after function name!");
            }
            t_arg = t_arg.next.next; //eat parL and get next char
            //
            Collections.List <Token> args = new Collections.List <Token>();
            while (t_arg.kind != 8)
            {
                switch (t_arg.kind)
                {
                case 1:
                    args.Add(new Token()
                    {
                        val = t_arg.val, kind = t_arg.kind
                    });
                    break;

                case 2:
                    args.Add(new Token()
                    {
                        val = t_arg.val, kind = t_arg.kind
                    });
                    break;

                case 3:
                    args.Add(ParseFunction(ref t_arg));
                    break;

                default:
                    throw new Exception("Unsupported character as argument!");
                }
                t_arg = t_arg.next;
            }
            char op = '\0';

            /*
             * List of functions. Some were added later on to be able to run certain test cases.
             * That might explain the messy appearance of this bit.. Highly influenced by foobar2000 scripting.
             */
            switch (fname)
            {
            case "if":
                if (args.Count < 2)
                {
                    throw new ArgumentException("Expected at least 2 arguments!");
                }
                return((this.ConvertToBool(GetValue(args[0]))) ? args[1] : new Token()
                {
                    val = String.Empty, kind = 1, line = 0, next = null
                });

            case "if2":
                if (args.Count < 3)
                {
                    throw new ArgumentException("Expected at least 3 arguments!");
                }
                return(new Token()
                {
                    val = (this.ConvertToBool(GetValue(args[0]))) ? GetValue(args[1]) : GetValue(args[2]), kind = 1
                });

            case "puts":
                if (args.Count < 2)
                {
                    throw new ArgumentException("Expected at least 2 arguments!");
                }
                if (args[0].kind != 2)
                {
                    throw new Exception("Expected a variable for the first argument");
                }
                _VARS[args[0].val] = GetValue(args[1]);
                return(new Token()
                {
                    kind = 1, val = ""
                });

            case "add":
                op = '+';
                goto case "mathop";

            case "sub":
                op = '-';
                goto case "mathop";

            case "mul":
                op = '*';
                goto case "mathop";

            case "div":
                op = '/';
                goto case "mathop";

            case "mathop":
            {
                if (args.Count < 2)
                {
                    throw new ArgumentException("Expected at least 2 arguments!");
                }
                double a, b;
                if (double.TryParse(GetValue(args[0]), out a) && double.TryParse(GetValue(args[1]), out b))
                {
                    switch (op)
                    {
                    case '+':
                        return(new Token()
                            {
                                kind = 1, val = (a + b).ToString()
                            });

                    case '-':
                        return(new Token()
                            {
                                kind = 1, val = (a - b).ToString()
                            });

                    case '/':
                        return(new Token()
                            {
                                kind = 1, val = (a / b).ToString()
                            });

                    case '*':
                        return(new Token()
                            {
                                kind = 1, val = (a * b).ToString()
                            });

                    default:
                        throw new ArgumentException("Invalid operator detected.");
                    }
                }
                else
                {
                    throw new ArgumentException("Non-numeric arguments detected.");
                }
            }

            case "eq":
                if (args.Count < 2)
                {
                    throw new ArgumentException("Expected at least 2 arguments!");
                }
                else if (args.Count == 2)
                {
                    return new Token()
                           {
                               kind = 1, val = GetValue(args[0]).Equals(GetValue(args[1])).ToString()
                           }
                }
                ;
                else      //more than 2 arguments
                {
                    int argnum = 1; bool state = false;
                    while (argnum < args.Count)
                    {
                        state = state | GetValue(args[0]).Equals(GetValue(args[argnum++]));
                    }
                    return(new Token()
                    {
                        kind = 1, val = state.ToString()
                    });
                }

            case "and":
                if (args.Count < 2)
                {
                    throw new ArgumentException("Expected at least 2 arguments!");
                }
                else if (args.Count == 2)
                {
                    return(new Token()
                    {
                        kind = 1,
                        val =
                            (Convert.ToBoolean(GetValue(args[0])) && Convert.ToBoolean(GetValue(args[1]))).ToString()
                    });
                }
                else      //more than 2 arguments
                {
                    int argnum = 1; bool state = true;
                    while (argnum < args.Count)
                    {
                        state = state & Boolean.Parse(GetValue(args[argnum++]));
                    }
                    return(new Token()
                    {
                        kind = 1, val = state.ToString()
                    });
                }

            case "cmp":
                if (args.Count < 3 || args.Count > 3)
                {
                    throw new ArgumentException("Expected 3 arguments!");
                }
                else
                {
                    int    scenario; bool result;
                    double arg0, arg1;
                    if (Int32.TryParse(GetValue(args[2]), out scenario) &&
                        Double.TryParse(GetValue(args[0]), out arg0) &&
                        Double.TryParse(GetValue(args[1]), out arg1))
                    {
                        switch (scenario)
                        {
                        case -1:
                            result = (arg0 < arg1);
                            break;

                        case 1:
                            result = (arg0 > arg1);
                            break;

                        default:
                            result = (arg0 == arg1);
                            break;
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Expected numerical arguments! Use eq function for string comparison!");
                    }
                    return(new Token()
                    {
                        kind = 1, val = result.ToString()
                    });
                }

            //IO Functions that were never implemented..
            case "open":
            {
                if (args.Count < 2)
                {
                    throw new ArgumentException("Expected at least 2 arguments!");
                }
                try
                {
                    IO.StreamReader temp = new IO.StreamReader(GetValue(args[1]));
                    _PIPES[args[0].val] = temp;
                }
                catch (IO.IOException ie) { throw ie; }
                return(new Token()
                    {
                        kind = 1, val = ""
                    });
            }

            case "close":
            {
                if (args.Count < 1)
                {
                    throw new ArgumentException("Expected 1 argument!");
                }
                IO.StreamReader temp;
                if (_PIPES.TryGetValue(GetValue(args[0]), out temp))
                {
                    temp.Close();
                    _PIPES.Remove(GetValue(args[0]));
                }
                return(new Token()
                    {
                        kind = 1, val = ""
                    });
            }

            default:
                throw new Exception("Unexistent function name.");
            }
        }