Beispiel #1
0
        static void Main(string[] args)
        {
            // TODO Later: コマンド解析処理
            if (args.Length != 1)
            {
                CompileError.Error("The number of command line argument must be 1.", true);
            }

            // TODO Later: ソースファイルのInput

            var code    = args[0];
            var context = new Context(code);
            var node    = new ProgramNode();

            node.Parse(context);

            var gen = new AssemblyGeneratorFactory().Create <X8664GenerateVisitor>();

            gen.Generate(node);

            // Epilogue
            gen.Add("    pop rax");
            gen.Add("    mov rsp, rbp");
            gen.Add("    pop rbp");
            gen.Add("    ret");

            gen.PutAll();
        }
Beispiel #2
0
        /// <summary>
        /// When the current token is a number, it consumes one token and returns that number.
        /// Otherwise it reports an error.
        /// </summary>
        public int ExpectNumber()
        {
            if (this.CurrentToken.TokenKind != TokenKind.NUMERIC)
            {
                CompileError.Error("Expect number", true);
            }
            var val = int.Parse(this.CurrentToken.TokenString);

            this.AdvanceToken();
            return(val);
        }
Beispiel #3
0
        /// <summary>
        /// When the current token kind is the expected kind, it consumes one token and returns that token.
        /// Otherwise it reports an error.
        /// </summary>
        public Token Expect(TokenKind tokenKind)
        {
            if (this.CurrentToken.TokenKind != tokenKind)
            {
                CompileError.Error("Expect Error", true);
            }
            var retval = this.CurrentToken;

            this.AdvanceToken();
            return(retval);
        }
Beispiel #4
0
        private Token[] Tokenize()
        {
            var tokens = new List <Token>();

            for (var p = 0; this.Text.Length > p;)
            {
                // Skip character
                if (SkipChars.Contains(this.Text[p]))
                {
                    p++;
                    continue;
                }
                // Muliti-letter punctuator
                if (this.Text.Length - p >= 2)
                {
                    var mlp = this.Text.Substring(p, 2);
                    if (ExistsKey(mlp))
                    {
                        var tk = ReverseReference(mlp);
                        tokens.Add(new Token(tk, mlp));
                        p += 2;
                        continue;
                    }
                }
                // Single-letter punctuator
                if (ExistsKey(this.Text[p].ToString()))
                {
                    var tk = ReverseReference(this.Text[p].ToString());
                    tokens.Add(new Token(tk, this.Text[p].ToString()));
                    p++;
                    continue;
                }
                // Integer literal
                var len = DigitLength(this.Text.Substring(p));
                if (len > 0)
                {
                    var token = new Token(TokenKind.NUMERIC, this.Text.Substring(p, len));
                    tokens.Add(token);
                    p += len;
                    continue;
                }
                // Identifier or keyword
                if (this.IsAllowedCharacter(this.Text[p]))
                {
                    var builder = new StringBuilder();
                    for (; this.IsAllowedCharacter(this.Text[p]); p++)
                    {
                        builder.Append(this.Text[p]);
                    }
                    var word = builder.ToString();

                    // Keyword
                    if (ExistsKey(word))
                    {
                        var tk = ReverseReference(word);
                        tokens.Add(new Token(tk, word));
                        continue;
                    }

                    // Identifier
                    var token = new Token(TokenKind.IDENTIFIER, word);
                    tokens.Add(token);
                    continue;
                }

                CompileError.Error("Couldn't tokenize.", true);
            }

            tokens.Add(new Token(TokenKind.EOF, "EOF"));
            return(tokens.ToArray());
        }