Пример #1
0
 /// <summary>
 /// Creates a new FunctionDeclaration given a token and a parent scope
 /// </summary>
 /// <param name="t">Function declaration token</param>
 /// <param name="parentScope">Parent scope of this function</param>
 public FunctionDeclaration(Token t, Scope parentScope)
     : this(parentScope)
 {
     // Initialize private members
     this.t = t;
     this.name = t.val;
 }
Пример #2
0
        void Get()
        {
            for (;;) {
            t = la;
            la = scanner.Scan();
            if (la.kind <= maxT) { ++errDist; break; }

            la = t;
            }
        }
Пример #3
0
        public void Parse()
        {
            la = new Token();
            la.val = "";
            Get();
            V();

            Expect(0);
        }
Пример #4
0
        /// <summary>
        /// Creates a new ConstantExpression given a primitive type and a token
        /// </summary>
        /// <param name="type">Constant type</param>
        /// <param name="t">Token containing value</param>
        public ConstantExpression(Primitive type, Token t)
        {
            // Set return type and token
            this.returnType = new PrimitiveType(type);
            this.t = t;

            // Processing is selected based on type
            switch (type)
            {
                // Process string
                case Primitive.String:
                    // Format string replacing escape characters
                    value = FormatString(t.val);
                    break;

                // Process integer
                case Primitive.Int:
                    try
                    {
                        // Attempt parsing the value of the token
                        value = Int32.Parse(t.val, NumberFormatInfo.InvariantInfo);
                    }
                    catch
                    {
                        value = 0;

                        // Invalid integer literal
                        Compiler.Compiler.errors.SemErr(t.line, t.col, "Invalid integer literal");
                    }
                    break;

                // Process double
                case Primitive.Double:
                    try
                    {
                        // Attempt parsing the value of the token
                        value = Double.Parse(t.val, NumberFormatInfo.InvariantInfo);
                    }
                    catch
                    {
                        value = 0;

                        // Invalid double literal
                        Compiler.Compiler.errors.SemErr(t.line, t.col, "Invalid double literal");
                    }
                    break;

                // Process boolean
                case Primitive.Bool:
                    try
                    {
                        // Attempt parsing the value of the token
                        value = Boolean.Parse(t.val);
                    }
                    catch
                    {
                        // Invalid boolean literal
                        Compiler.Compiler.errors.SemErr(t.line, t.col, "Invalid boolean literal");
                    }
                    break;
                default:
                    break;
            }
        }
Пример #5
0
 /// <summary>
 /// Creates a new CallExpression given a method name and a token
 /// </summary>
 /// <param name="methodName">Method name</param>
 /// <param name="t">Call token</param>
 public CallExpression(string methodName, Token t)
     : this()
 {
     // Set method name and token
     this.methodName = methodName;
     this.t = t;
 }
Пример #6
0
        Token NextToken()
        {
            while (ch == ' ' ||
            ch >= 9 && ch <= 10 || ch == 13
            ) NextCh();
            if (ch == '/' && Comment0() ||ch == '/' && Comment1()) return NextToken();
            int apx = 0;
            t = new Token();
            t.pos = pos; t.col = col; t.line = line;
            int state;
            try { state = start[ch]; } catch (KeyNotFoundException) { state = 0; }
            tlen = 0; AddCh();

            switch (state) {
            case -1: { t.kind = eofSym; break; } // NextCh already done
            case 0: { t.kind = noSym; break; }   // NextCh already done
            case 1:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch == '_' || ch >= 'a' && ch <= 'z' || ch == 160 || ch == 170 || ch == 181 || ch == 186 || ch >= 192 && ch <= 214 || ch >= 216 && ch <= 246 || ch >= 248 && ch <= 255) {AddCh(); goto case 1;}
                else if (ch == 92) {AddCh(); goto case 2;}
                else {t.kind = 1; t.val = new String(tval, 0, tlen); CheckLiteral(); return t;}
            case 2:
                if (ch == 'u') {AddCh(); goto case 3;}
                else if (ch == 'U') {AddCh(); goto case 7;}
                else {t.kind = noSym; break;}
            case 3:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 4;}
                else {t.kind = noSym; break;}
            case 4:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 5;}
                else {t.kind = noSym; break;}
            case 5:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 6;}
                else {t.kind = noSym; break;}
            case 6:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 1;}
                else {t.kind = noSym; break;}
            case 7:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 8;}
                else {t.kind = noSym; break;}
            case 8:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 9;}
                else {t.kind = noSym; break;}
            case 9:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 10;}
                else {t.kind = noSym; break;}
            case 10:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 11;}
                else {t.kind = noSym; break;}
            case 11:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 12;}
                else {t.kind = noSym; break;}
            case 12:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 13;}
                else {t.kind = noSym; break;}
            case 13:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 14;}
                else {t.kind = noSym; break;}
            case 14:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 1;}
                else {t.kind = noSym; break;}
            case 15:
                if (ch == 'u') {AddCh(); goto case 16;}
                else if (ch == 'U') {AddCh(); goto case 20;}
                else {t.kind = noSym; break;}
            case 16:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 17;}
                else {t.kind = noSym; break;}
            case 17:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 18;}
                else {t.kind = noSym; break;}
            case 18:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 19;}
                else {t.kind = noSym; break;}
            case 19:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 1;}
                else {t.kind = noSym; break;}
            case 20:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 21;}
                else {t.kind = noSym; break;}
            case 21:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 22;}
                else {t.kind = noSym; break;}
            case 22:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 23;}
                else {t.kind = noSym; break;}
            case 23:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 24;}
                else {t.kind = noSym; break;}
            case 24:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 25;}
                else {t.kind = noSym; break;}
            case 25:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 26;}
                else {t.kind = noSym; break;}
            case 26:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 27;}
                else {t.kind = noSym; break;}
            case 27:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 1;}
                else {t.kind = noSym; break;}
            case 28:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 29;}
                else {t.kind = noSym; break;}
            case 29:
                if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'F' || ch >= 'a' && ch <= 'f') {AddCh(); goto case 29;}
                else {t.kind = 2; break;}
            case 30:
                {
                    tlen -= apx;
                    buffer.Pos = t.pos; NextCh(); line = t.line; col = t.col;
                    for (int i = 0; i < tlen; i++) NextCh();
                    t.kind = 2; break;}
            case 31:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 31;}
                else if (ch == 'E' || ch == 'e') {AddCh(); goto case 32;}
                else {t.kind = 3; break;}
            case 32:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 34;}
                else if (ch == '+' || ch == '-') {AddCh(); goto case 33;}
                else {t.kind = noSym; break;}
            case 33:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 34;}
                else {t.kind = noSym; break;}
            case 34:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 34;}
                else {t.kind = 3; break;}
            case 35:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 35;}
                else if (ch == 'E' || ch == 'e') {AddCh(); goto case 36;}
                else {t.kind = 3; break;}
            case 36:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 38;}
                else if (ch == '+' || ch == '-') {AddCh(); goto case 37;}
                else {t.kind = noSym; break;}
            case 37:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 38;}
                else {t.kind = noSym; break;}
            case 38:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 38;}
                else {t.kind = 3; break;}
            case 39:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 41;}
                else if (ch == '+' || ch == '-') {AddCh(); goto case 40;}
                else {t.kind = noSym; break;}
            case 40:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 41;}
                else {t.kind = noSym; break;}
            case 41:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 41;}
                else if (ch == 'D' || ch == 'd') {AddCh(); goto case 42;}
                else {t.kind = 3; break;}
            case 42:
                {t.kind = 3; break;}
            case 43:
                if (ch <= 9 || ch >= 11 && ch <= 12 || ch >= 14 && ch <= '!' || ch >= '#' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); goto case 43;}
                else if (ch == '"') {AddCh(); goto case 44;}
                else if (ch == 92) {AddCh(); goto case 64;}
                else {t.kind = noSym; break;}
            case 44:
                {t.kind = 4; break;}
            case 45:
                {t.kind = 30; break;}
            case 46:
                {t.kind = 31; break;}
            case 47:
                {t.kind = 33; break;}
            case 48:
                {t.kind = 35; break;}
            case 49:
                {t.kind = 36; break;}
            case 50:
                {t.kind = 37; break;}
            case 51:
                {t.kind = 38; break;}
            case 52:
                {t.kind = 40; break;}
            case 53:
                {t.kind = 42; break;}
            case 54:
                {t.kind = 43; break;}
            case 55:
                {t.kind = 44; break;}
            case 56:
                {t.kind = 45; break;}
            case 57:
                {t.kind = 46; break;}
            case 58:
                {t.kind = 47; break;}
            case 59:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 59;}
                else if (ch == '.') {apx++; AddCh(); goto case 65;}
                else if (ch == 'E' || ch == 'e') {AddCh(); goto case 39;}
                else if (ch == 'D' || ch == 'd') {AddCh(); goto case 42;}
                else {t.kind = 2; break;}
            case 60:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 59;}
                else if (ch == '.') {apx++; AddCh(); goto case 65;}
                else if (ch == 'X' || ch == 'x') {AddCh(); goto case 28;}
                else if (ch == 'E' || ch == 'e') {AddCh(); goto case 39;}
                else if (ch == 'D' || ch == 'd') {AddCh(); goto case 42;}
                else {t.kind = 2; break;}
            case 61:
                if (ch >= '0' && ch <= '9') {AddCh(); goto case 31;}
                else {t.kind = 32; break;}
            case 62:
                if (ch == '=') {AddCh(); goto case 47;}
                else {t.kind = 29; break;}
            case 63:
                if (ch == '=') {AddCh(); goto case 48;}
                else {t.kind = 34; break;}
            case 64:
                if (ch == '"' || ch == 39 || ch == '0' || ch == 92 || ch >= 'a' && ch <= 'b' || ch == 'f' || ch == 'n' || ch == 'r' || ch == 't' || ch == 'v') {AddCh(); goto case 43;}
                else {t.kind = noSym; break;}
            case 65:
                if (ch <= '/' || ch >= ':' && ch <= 65535) {apx++; AddCh(); goto case 30;}
                else if (ch >= '0' && ch <= '9') {apx = 0; AddCh(); goto case 35;}
                else {t.kind = noSym; break;}
            case 66:
                {t.kind = 48; break;}
            case 67:
                {t.kind = 49; break;}
            case 68:
                {t.kind = 50; break;}
            case 69:
                {t.kind = 51; break;}
            case 70:
                if (ch == '=') {AddCh(); goto case 67;}
                else {t.kind = 39; break;}
            case 71:
                if (ch == '=') {AddCh(); goto case 66;}
                else {t.kind = 41; break;}

            }
            t.val = new String(tval, 0, tlen);
            return t;
        }
Пример #7
0
        void Init()
        {
            pos = -1; line = 1; col = 0;
            oldEols = 0;
            NextCh();
            if (ch == 0xEF) { // check optional byte order mark for UTF-8
            NextCh(); int ch1 = ch;
            NextCh(); int ch2 = ch;
            if (ch1 != 0xBB || ch2 != 0xBF) {
                throw new FatalError(String.Format("illegal byte order mark: EF {0,2:X} {1,2:X}", ch1, ch2));
            }
            buffer = new UTF8Buffer(buffer); col = 0;
            NextCh();
            }
            start = new Dictionary<int, int>(128);
            for (int i = 65; i <= 90; ++i) start[i] = 1;
            for (int i = 95; i <= 95; ++i) start[i] = 1;
            for (int i = 97; i <= 122; ++i) start[i] = 1;
            for (int i = 170; i <= 170; ++i) start[i] = 1;
            for (int i = 181; i <= 181; ++i) start[i] = 1;
            for (int i = 186; i <= 186; ++i) start[i] = 1;
            for (int i = 192; i <= 214; ++i) start[i] = 1;
            for (int i = 216; i <= 246; ++i) start[i] = 1;
            for (int i = 248; i <= 255; ++i) start[i] = 1;
            for (int i = 49; i <= 57; ++i) start[i] = 59;
            start[92] = 15;
            start[48] = 60;
            start[46] = 61;
            start[34] = 43;
            start[61] = 62;
            start[58] = 45;
            start[44] = 46;
            start[62] = 63;
            start[123] = 49;
            start[91] = 50;
            start[40] = 51;
            start[60] = 70;
            start[45] = 52;
            start[33] = 71;
            start[43] = 53;
            start[125] = 54;
            start[93] = 55;
            start[41] = 56;
            start[59] = 57;
            start[42] = 58;
            start[47] = 68;
            start[37] = 69;
            start[Buffer.EOF] = -1;

            pt = tokens = new Token();  // first token is a dummy
        }
Пример #8
0
 // get the next token (possibly a token already seen during peeking)
 public Token Scan()
 {
     if (tokens.next == null) {
     return NextToken();
     } else {
     pt = tokens = tokens.next;
     return tokens;
     }
 }
Пример #9
0
 // make sure that peeking starts at the current scan position
 public void ResetPeek()
 {
     pt = tokens;
 }
Пример #10
0
 // peek for the next token, ignore pragmas
 public Token Peek()
 {
     if (pt.next == null) {
     do {
         pt = pt.next = NextToken();
     } while (pt.kind > maxT); // skip pragmas
     } else {
     do {
         pt = pt.next;
     } while (pt.kind > maxT);
     }
     return pt;
 }
Пример #11
0
 /// <summary>
 /// Creates a new VariableDeclaration given a token and a type
 /// </summary>
 /// <param name="t">Declaration token</param>
 /// <param name="type">Variable type</param>
 public VariableDeclaration(Token t, BaseType type)
 {
     // Set members
     this.t = t;
     this.name = t.val;
     this.type = type;
 }
Пример #12
0
 /// <summary>
 /// Adds an argument to the function's argument list
 /// </summary>
 /// <param name="t">Argument token</param>
 /// <param name="type">Argument type</param>
 public void AddArgument(Token t, BaseType type)
 {
     // Add argument to the local symbol table as a variable declaration
     localScope.AddArgument(new VariableDeclaration(t, type));
 }