예제 #1
0
 public object TryResolveSymbol(SymbolToken pSymbolToken)
 {
     if (vars.ContainsKey(pSymbolToken.value)) {
         //Console.WriteLine("Resolved " + pSymbolToken + " in scope " + name);
         return vars[pSymbolToken.value];
     } else if (enclosingScope != null) {
         //Console.WriteLine("Can't find " + pSymbolToken + " in scope " + name + ", looking in enclosing scope: " + enclosingScope.name);
         return enclosingScope.TryResolveSymbol (pSymbolToken);
     } else {
         throw new Exception ("Can't resolve symbol " + pSymbolToken + " at line " + pSymbolToken.line + " and position " + pSymbolToken.position);
     }
 }
예제 #2
0
        public List<Token> Read(string pString)
        {
            _readPosition = 0;
            _stringToRead = pString;
            _tokens = new List<Token> ();

            while(MoreToRead()) {
                char c = ReadCurrentChar ();

                Token newToken = null;

                int lineBeforeReading = _lineNr;
                int positionBeforeReading = _positionOnLine;

                if (c == '\n') {
                    _positionOnLine = 1;
                    _lineNr++;
                    Step ();
                }
                else if (IsWhitespace (c)) {
                    Step ();
                } else if (c == '(') {
                    newToken = new Token (TokenType.LEFT_PAREN);
                    Step ();
                } else if (c == ')') {
                    newToken = new Token(TokenType.RIGHT_PAREN);
                    Step ();
                } else if (c == '[') {
                    newToken = new Token(TokenType.LEFT_BRACKET);
                    Step ();
                } else if (c == ']') {
                    newToken = new Token(TokenType.RIGHT_BRACKET);
                    Step ();
                } else if (c == '\'') {
                    newToken = new Token(TokenType.TICK);
                    Step ();
                } else if (c == ';') {
                    while (MoreToRead() && ReadCurrentChar() != '\n') {
                        Step ();
                    }
                } else if (c == '\"') {
                    newToken = new StringToken(ReadString ());
                } else if (c == '&') {
                    newToken = new ReservedToken(TokenType.AMPERSAND);
                    Step ();
                } else if (c == '.') {
                    newToken = new SymbolToken(c.ToString());
                    Step ();
                } else if(IsChar (c)) {
                    string text = ReadText ();
                    if(IsNumber(text)) {
                        newToken = new FloatToken(Convert.ToSingle(text));
                    } else if (text == "def") {
                        newToken = new ReservedToken(TokenType.DEF);
                    } else if (text == "fn") {
                        newToken = new ReservedToken(TokenType.FN);
                    } else if (text == "let") {
                        newToken = new ReservedToken(TokenType.LET);
                    } else if (text == "if") {
                        newToken = new ReservedToken(TokenType.IF);
                    } else if (text == "defmacro") {
                        newToken = new ReservedToken(TokenType.DEFMACRO);
                    } else if (text == "quote") {
                        newToken = new ReservedToken(TokenType.QUOTE);
                    } else if (text == "set!") {
                        newToken = new ReservedToken(TokenType.SET);
                    } else if (text == "null") {
                        newToken = new NullToken();
                    } else if (text == "true") {
                        newToken = new BoolToken(true);
                    } else if (text == "false") {
                        newToken = new BoolToken(false);
                    } else {
                        newToken = new SymbolToken(text);
                    }
                }
                else {
                    throw new Exception ("Can't understand char '" + c + "'" + " at line " + _lineNr + " and position " + _positionOnLine);
                }

                if(newToken != null) {
                    newToken.line = lineBeforeReading;
                    newToken.position = positionBeforeReading;
                    _tokens.Add (newToken);
                }
            }

            return _tokens;
        }