Ejemplo n.º 1
0
        public static Token GetCmdLnArgs(string[] args)
        {
            if (args == null)
                throw new ArgumentNullException("args");

            List<Token> opts = new List<Token>();
            List<Token> srcs = new List<Token>();

            Action<string> Pick = delegate(string arg)
            {
                if (arg == null) return;
                arg = arg.Trim();
                if (arg == "") return;

                Token t;
                if (Regex.IsMatch(arg, OptHead))
                {
                    if ((t = PickOpt(arg)) == null)
                    {
                        throw new Exception(string.Format("Not supported option: {0}", arg));
                    }
                    opts.Add(t);
                }
                else
                {
                    srcs.Add(PickSrc(arg));
                }
            };

            foreach (string arg in args)
            {
                Pick(arg);
            }

            Token srct = new Token("", "Sources");
            srct.Follows = srcs.ToArray();

            Token optt = new Token("", "CompileOptions");
            optt.Follows = opts.ToArray();

            if (0 == optt.Select("out").Length && srcs.Count > 0)
            {
                string v = srcs[0].Value;
                string dir = System.IO.Path.GetDirectoryName(v);
                string fn = System.IO.Path.GetFileNameWithoutExtension(v) + ".exe";
                optt.FlwsAdd(NewOpt("out", System.IO.Path.Combine(dir, fn)));
            }

            Token ret = new Token("", "Arguments");
            ret.Follows = new Token[] { optt, srct };

            return ret;
        }
Ejemplo n.º 2
0
        public static void AnalyzeSyntax(Token root)
        {
            SyntaxAnalyzer analyzer = new SyntaxAnalyzer();

            Token srcs = root.Find("Sources");

            Token syntax = root.Find("Syntax");
            List<Token> synflw = new List<Token>();
            foreach (Token f in srcs.Follows)
            {
                if (f.Group == "SourcePath") { continue; }
                if (f.Group == "SourceText") { synflw.Add(analyzer.Run(f.Value, f.First.Value)); }
            }
            syntax.Follows = synflw.ToArray();
        }
Ejemplo n.º 3
0
        public static void Check(Token root)
        {
            if (root == null) { throw new ArgumentNullException("args"); }

            if (0 == root.Select("CompileOptions").Length) { throw new ArgumentException("No @CompileOptions Token"); }
            if (1 < root.Select("CompileOptions").Length) { throw new ArgumentException("Too many @CompileOptions Token"); }

            if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length)
            { throw new ArgumentException("No source filename is specified to command line parameter"); }

            if (1 < root.Select("Sources").Length) { throw new ArgumentException("Too many @Sources Token"); }

            if (0 == root.Select("CompileOptions/@out").Length)
            {
                if (0 == root.Select("Sources").Length
                    || 0 == root.Find("Sources").Follows.Length)
                {
                    throw new ArgumentException("Cannot omit source path when out option was omitted");
                }
            }

            if (1 < root.Select("CompileOptions/out").Length) { throw new ArgumentException("Too many out options are specified to command line parameter"); }

            if (1 == root.Select("Sources").Length)
            {
                foreach (Token p in root.Select("Sources/SourcePath"))
                {
                    if (false == File.Exists(p.Value))
                    {
                        if (false == File.Exists(p.Value + ".nana"))
                        { throw new FileNotFoundException("Source file was not found", p.Value); }
                        else
                        { p.Value += ".nana"; }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public static Token CreateParamToken(string[] nameAndTypes)
        {
            Token cur = null;
            Token cma = null;
            foreach (string nt in nameAndTypes)
            {
                if (null != cma)
                {
                    Token tmp = cma;
                    cma = new Token(",", "_End_Cma_");
                    cma.First = tmp;
                }

                string[] spl = nt.Split(new char[] { ':' });
                cur = new Token(":", "TypSpc");
                cur.First = new Token(spl[0], "Id");
                cur.Second = new Token(spl[1], "Id");

                if (cma == null)
                { cma = cur; }
                else
                { cma.Second = cur; }
            }

            return cma;
        }
Ejemplo n.º 5
0
 public EnvAnalyzer(Token seed)
     : base(seed, /*above*/ BlkAnalyzer.EmptyBlz)
 {
     Ez = this;
 }
Ejemplo n.º 6
0
 public AccessError(string message, Token t)
     : base(message, t.Path, t.Row, t.Col)
 {
 }
Ejemplo n.º 7
0
 public IMRTranslation(string message, Token t)
     : base(message, t.Path, t.Row, t.Col)
 {
 }
Ejemplo n.º 8
0
        public void Next()
        {
            _Cur = NoMore;
            Token cur = null;

            do
            {
                if (R.BOF)
                {
                    CallReadLine();
                    if (R.EOF) return;
                }

                SkipSpace();
                while (EOL)
                {
                    CallReadLine();
                    if (R.EOF) return;
                    SkipSpace();
                }

                cur = GetToken();

            } while (SkipGroups.Contains(cur.Group));

            _Cur = cur;
        }
Ejemplo n.º 9
0
 public SemanticError(string message, Token t)
     : base(message, t)
 {
 }
Ejemplo n.º 10
0
        public override Token GetToken()
        {
            string ln = SubCurLine;
            if (IsTokenStart() == false) throw new Exception();
            Match startMt = StartRx.Match(ln);
            Token t = new Token();
            t.Group = Group;

            StringBuilder b = new StringBuilder();
            Match escMt = null, endMt = null;
            int startIdx = startMt.Length;

            Action match = delegate()
            {
                escMt = EscRx.Match(ln, startIdx);
                endMt = EndRx.Match(ln, startIdx);
                if (endMt.Success)
                {
                    if (escMt.Success && escMt.Index < endMt.Index)
                    {
                        endMt = null;
                        startIdx = escMt.Index + escMt.Length;
                    }
                }
                else
                {
                    endMt = null;
                    startIdx = ln.Length;
                }
            };

            match();
            while (ln != null && endMt == null)
            {
                if (startIdx >= ln.Length)
                {
                    b.AppendLine(ln);
                    CallReadLine();
                    if (R.EOF) break;
                    ln = SubCurLine;
                    startIdx = 0;
                }
                match();
            }

            if (endMt == null) throw new Exception(@"No end");
            b.Append(ln.Substring(0, endMt.Index + endMt.Length));
            t.Value = b.ToString();
            Pos.Value += endMt.Index + endMt.Length;

            return t;
        }
Ejemplo n.º 11
0
 public Prepend(ITokenEnumerator enm, Token value)
 {
     IsFirst = true;
     Enm = enm;
     Value = value;
 }
Ejemplo n.º 12
0
 public static Token CreateClassToken(string name, string basename)
 {
     Token  root  = new Token("class", "Typ");
     root.FlwsAdd(name, "Name");
     if (null != basename)
     {
         Token bs = new Token("->", "BaseTypeDef");
         bs.FlwsAdd(basename, "Id");
         root.FlwsAdd(bs);
     }
     root.FlwsAdd("...", "Block")
         .FlwsAdd(",,,");
     return root;
 }
Ejemplo n.º 13
0
        public static Token CreateFunToken(string func, string name, string returnType)
        {
            Debug.Assert(false == string.IsNullOrEmpty(func));

            Token f;

            f = new Token(func, "Fun");

            if (string.IsNullOrEmpty(name) == false)
            { f.FlwsAdd(name, "Name"); }

            f.FlwsAdd("(", "Prm").FlwsAdd(")");

            if (string.IsNullOrEmpty(returnType) == false)
            {
                f.FlwsAdd(":", "TypSpc");
                f.FlwsTail.FlwsAdd(returnType, "Id");
            }

            f.FlwsAdd("..", "Block");
            f.FlwsTail.Follows = new Token[0];

            return f;
        }
Ejemplo n.º 14
0
        public LineAnalyzer(Token seed, BlkAnalyzer above)
        {
            Seed = seed;
            Above = above;

            Breaks = new Stack<Literal>();
            Continues = new Stack<Literal>();

            if (null != above && null != above.Ez)
            { E = Above.Ez.E; }
        }
Ejemplo n.º 15
0
 public BlkAnalyzer NewBlz(Token t)
 {
     BlkAnalyzer blz = new BlkAnalyzer(t, this);
     Blzs.AddLast(blz);
     return blz;
 }
Ejemplo n.º 16
0
 public static string AnalyzeName(string ftyp, Token seed)
 {
     string nameasm = null;
     if (ftyp == "cons") { nameasm = Nana.IMRs.IMRGenerator.InstCons; }
     if (ftyp == "scons") { nameasm = Nana.IMRs.IMRGenerator.StatCons; }
     if (ftyp == "sfun" || ftyp == "nfun" || ftyp == "vfun") { nameasm = seed.Follows[0].Value; }
     return nameasm;
 }
Ejemplo n.º 17
0
 public FunAnalyzer(Token seed, BlkAnalyzer above)
     : base(seed, above)
 {
     CopyAboveAnalyzers(above);
     Blz = Fuz = this;
 }
Ejemplo n.º 18
0
 public static Env Run(Token root)
 {
     EnvAnalyzer ez = new EnvAnalyzer(root);
     ez.AnalyzeEnv();
     return ez.E;
 }
Ejemplo n.º 19
0
        public override Token GetToken()
        {
            string ln = SubCurLine;
            if (IsTokenStart() == false) throw new Exception();
            string[] groups;
            string g = "";
            Match m = StartRx.Match(ln);
            Token t = new Token();
            groups = StartRx.GetGroupNames();
            for (int i = 1; i < groups.Length; i++)
            {
                if (m.Groups[i].Success)
                {
                    g = groups[i];
                    break;
                }
            }

            t = new Token();
            t.Value = m.Value;
            t.Group = g;
            t.Path = R.Path;
            t.Row = R.ReadCount;
            t.Col = Pos.Value;

            Pos.Value += m.Index + m.Length;

            return t;
        }
Ejemplo n.º 20
0
 public static Token CreateVarToken(Variable v)
 {
     Token t = new Token(":", "TypSpc");
     t.First = new Token(v.Name, "Id");
     t.Second = new Token(v.Att.TypGet.Name, "Id");
     return t;
 }
Ejemplo n.º 21
0
 public TokenizerBase(Regex startRx, List<string> skipGroups)
 {
     _Cur = null;
     StartRx = startRx;
     SkipGroups = skipGroups;
 }
Ejemplo n.º 22
0
        public static void Prepare(Token root)
        {
            if (false == root.Contains("Syntax"))
            { root.FlwsAdd(new Token("", "Syntax")); }

            if (false == root.Contains("Code"))
            { root.FlwsAdd(new Token("", "Code")); }
        }
Ejemplo n.º 23
0
 public Append(ITokenEnumerator enm, Token value)
 {
     IsInnerEOF = false;
     Enm = enm;
     Value = value;
 }
Ejemplo n.º 24
0
        public static void ReadSourceFiles(Token root)
        {
            Token srcs = root.Find("Sources");

            UTF8Encoding utf8 = new UTF8Encoding(false /* no byte order mark */);
            List<Token> srcsflw = new List<Token>();
            foreach (Token f in srcs.Follows)
            {
                srcsflw.Add(f);
                if (f.Group == "SourceText")
                {
                    f.First = new Token("");
                    continue;
                }
                if (f.Group == "SourcePath")
                {
                    string text = File.ReadAllText(f.Value, utf8);
                    Token txtt = new Token(text, "SourceText");
                    txtt.First = f;
                    srcsflw.Add(txtt);
                }
            }
            srcs.Follows = srcsflw.ToArray();
        }
Ejemplo n.º 25
0
 public SyntaxError(string message, Token t)
     : base(message, t.Path, t.Row, t.Col)
 {
 }
Ejemplo n.º 26
0
        public void Compile(Token root)
        {
            Prepare(root);

            Token srcs = root.Find("Sources");

            //  append SourceText if it's SourcePath
            ReadSourceFiles(root);

            AnalyzeSyntax(root);

            AfterSyntaxAnalyze(root);

            Env env =  AnalyzeSemantic(root);

            AfterSemanticAnalyze(root, env);

            IMRGenerator imrgen = new IMRGenerator();
            imrgen.GenerateIMR(env.Ap);

            Token code = root.Find("Code");
            CodeGenerator codegen = new CodeGenerator();
            code.Value = codegen.GenerateCode(env);
        }
Ejemplo n.º 27
0
 public Error(string message, Token t)
     : this(message, t.Path, t.Row, t.Col)
 {
 }
Ejemplo n.º 28
0
 public static Env AnalyzeSemantic(Token root)
 {
     return EnvAnalyzer.Run(root);
 }
Ejemplo n.º 29
0
 public InternalError(string message, Token t)
     : base(message, t.Path, t.Row, t.Col)
 {
 }
Ejemplo n.º 30
0
 public CustomAnalyzer(Token seed, BlkAnalyzer above)
     : base(seed, above)
 {
 }