MESSAGE
DATE | 2016-02-21 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Hangout-NYLXS] Antlr
|
From hangout-bounces-at-nylxs.com Sun Feb 21 04:12:40 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 0BE2F163D98; Sun, 21 Feb 2016 04:12:25 -0500 (EST) X-Original-To: hangout-at-nylxs.com Delivered-To: hangout-at-nylxs.com Received: from [10.0.0.19] (stat13.mrbrklyn.com [10.0.0.19]) by mrbrklyn.com (Postfix) with ESMTP id 56B181616D8 for ; Sun, 21 Feb 2016 03:40:34 -0500 (EST) To: Hangout From: Ruben Safir Message-ID: <56C97802.6080004-at-mrbrklyn.com> Date: Sun, 21 Feb 2016 03:40:34 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------030007050405090704000103" Subject: [Hangout-NYLXS] Antlr X-BeenThere: hangout-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list Reply-To: NYLXS Discussions List List-Id: NYLXS Discussions List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: hangout-bounces-at-nylxs.com Sender: "hangout"
This is a multi-part message in MIME format. --------------030007050405090704000103 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit
anyone try to use Antlr yet? It is a decent gammer parser. I got it working on the command line, but it has a working plugin for netbeans
We've been writing lexers and parser in Java so far with a gradle compiler for java. Java is really a PIA.
this is not all my code, but you can get an idea of how the compiler shapes up
Ruben -- So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! http://www.brooklyn-living.com
Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013
--------------030007050405090704000103 Content-Type: text/x-java; name="Interpreter.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Interpreter.java"
package pico;
import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigInteger; import java.util.HashMap; import java.util.Stack; import java.util.ArrayList; import java.util.Iterator; import java.util.Collections;
public class Interpreter {
Stack stack = new Stack();//Where is the keyword Value defined? HashMap dict = new HashMap<>(); //symbols table (dynamic)
public void interpret(TokenSource lists_of_stuff) { for(Token token = lists_of_stuff.nextToken(); token.type != Token.Type.EOF; token = lists_of_stuff.nextToken()) { switch(token.type) { case INT: case STR: case SYM: stack.push(new Value(token)); //sends token to Value break; case LBRACE: stack.push(new Value(lists_of_stuff));// sends a break; case OP: // First check the dictionary. Value v = dict.get(token.text); //This is hash behavior if(v != null) { if(v.type == Value.Type.BLOCK) { interpret(v.asTokenSource()); //RECURSIVE CALL } else { stack.push(v); } } else { try { // Invoke by reflection -- looks for a public void // method with same name as the operator. Method m = Interpreter.class.getMethod("__" + token.text); m.invoke(this); } catch (NoSuchMethodException e) { throw new RuntimeError("Unknown operator: " + token.text); } catch (IllegalAccessException e) { throw new RuntimeError(e); } catch (InvocationTargetException e) { throw new RuntimeError(e + token.text ); } } break; default: throw new RuntimeError("Unhandled token type: " + token.type); } } }
public void __add() { BigInteger y = stack.pop().asInteger(); BigInteger x = stack.pop().asInteger(); stack.push(new Value(x.add(y))); }
public void __idiv() { BigInteger y = stack.pop().asInteger(); BigInteger x = stack.pop().asInteger(); stack.push(new Value(x.divide(y))); }
public void __mul() { BigInteger y = stack.pop().asInteger(); BigInteger x = stack.pop().asInteger(); stack.push(new Value(x.multiply(y))); }
public void __sub() { BigInteger y = stack.pop().asInteger(); BigInteger x = stack.pop().asInteger(); stack.push(new Value(x.subtract(y))); }
public void __mod() { BigInteger y = stack.pop().asInteger(); BigInteger x = stack.pop().asInteger(); stack.push(new Value(x.mod(y))); } public void __length() { Value str_val_tok = stack.pop(); String str_1 = str_val_tok.text; stack.push( new Value( BigInteger.valueOf( (str_1.length() ) ) ) ); }
public void __get() { BigInteger pos = stack.pop().asInteger(); Value str_val_tok = stack.pop(); String str_2 = str_val_tok.text; int stop = str_2.length(); System.out.println( "stop ==>" + stop + "pos ==> " + pos + ":" + str_2); if(pos.intValue() < stop){ char tmp = str_2.charAt(pos.intValue()); System.out.println( "tmp ==>" + tmp + "pos ==> " + pos + ":" + str_2); stack.push( new Value( BigInteger.valueOf( (int) tmp ))); } else{ throw new RuntimeError("strewed"); } }
public void __strcat(){ Value str_val_tok = stack.pop(); Value str_val_tok2 = stack.pop(); String str_3 = str_val_tok.text; String str_4 = str_val_tok2.text; String whole_string = new StringBuffer().append(str_4).append(str_3).toString(); Token tmp_tok = new Token( Token.Type.STR, whole_string ); stack.push(new Value(tmp_tok)); }
public void __tostr(){ Value unknown_tok = stack.pop(); if(unknown_tok.type == Value.Type.BOOL ){ String tmp = Boolean.toString(unknown_tok.truth); Token tmp_lextok = new Token(Token.Type.STR,tmp); stack.push(new Value(tmp_lextok)); }else if(unknown_tok.type == Value.Type.INT){ String tmp = unknown_tok.integer.toString(10); Token tmp_lextok = new Token(Token.Type.STR,tmp); stack.push(new Value(tmp_lextok)); } else{ throw new RuntimeError("Unhandled token type: " + unknown_tok.type); } }
public void __tochar(){ Value unknown_tok = stack.pop(); if(unknown_tok.type == Value.Type.INT){ if(unknown_tok.integer.intValue() > 2097152 ){ throw new RuntimeError("Unhandled token value Greater than 2097152 " + unknown_tok.integer.intValue()); } int ascii = unknown_tok.integer.intValue(); char letter = (char)ascii; String tmp = Character.toString(letter); stack.push(new Value(new Token(Token.Type.STR, tmp))); }else{ throw new RuntimeError("Unhandled token type: " + unknown_tok.type); } }
public void __true(){ System.out.println("Welcome to __true"); stack.push(new Value(true)); } public void __false(){ System.out.println("Welcome to __false"); stack.push(new Value(false)); }
public void __eq(){ int t = __cmp(); if(t == 0){ stack.push(new Value(true)); } else{ stack.push(new Value(false)); } }
public void __ne(){ int t = __cmp(); if(t == 0){ stack.push(new Value(false)); } else{ stack.push(new Value(true)); } }
public void __ge(){ int t = __cmp(); if (t == -99){ throw new RuntimeError("Wrong Compare Operand Types"); }else{ if(t == 0){ stack.push(new Value(true)); }else if(t > 0){ stack.push(new Value(true)); }else if(t < 0 ){ stack.push(new Value(false)); }else{ throw new RuntimeError("Nomans land " + t); } }
}
public void __gt(){ int t = __cmp(); if (t == -99){ throw new RuntimeError("Wrong Compare Operand Types"); }else{ if(t == 0){ stack.push(new Value(false)); }else if(t > 0){ stack.push(new Value(true)); }else if(t < 0 ){ stack.push(new Value(false)); }else{ throw new RuntimeError("Nomans land " + t); } }
} public void __le(){ int t = __cmp(); if (t == -99){ throw new RuntimeError("Wrong Compare Operand Types"); }else{ if(t == 0){ stack.push(new Value(true)); }else if(t > 0){ stack.push(new Value(false)); }else if(t < 0 ){ stack.push(new Value(true)); }else{ throw new RuntimeError("Nomans land " + t); } }
} public void __lt(){ int t = __cmp(); if (t == -99){ throw new RuntimeError("Wrong Compare Operand Types"); }else{ if(t == 0){ stack.push(new Value(false)); }else if(t > 0){ stack.push(new Value(false)); }else if(t < 0 ){ stack.push(new Value(true)); }else{ throw new RuntimeError("Nomans land " + t); } } }
public void __and(){ Value tmp1 = stack.pop(); Value tmp2 = stack.pop(); if((tmp1.type != Value.Type.BOOL) && (tmp2.type != Value.Type.BOOL)){ throw new RuntimeError("Wrong Compare Operator types - Not BOOL"); } else{ if((tmp1.truth == true) && (tmp2.truth == true) ){ stack.push(new Value(true)); }else{ stack.push(new Value(false)); } } } public void __or(){ Value tmp1 = stack.pop(); Value tmp2 = stack.pop(); if((tmp1.type != Value.Type.BOOL) && (tmp2.type != Value.Type.BOOL)){ throw new RuntimeError("Wrong Compare Operator types - Not BOOL"); } else{ if((tmp1.truth == true) || (tmp2.truth == true) ){ stack.push(new Value(true)); }else{ stack.push(new Value(false)); } } } public void __not(){ Value tmp1 = stack.pop(); if(tmp1.type != Value.Type.BOOL ){ throw new RuntimeError("Wrong Compare Operator types - Not BOOL"); } else{ if(tmp1.truth == true ){ stack.push(new Value(false)); }else{ stack.push(new Value(true)); } } }
public void __if(){ //Takes 2 values. The second is a BOOL value. If the second one is true, then run the first //which might be a block Value tmp1 = stack.pop(); Value tmp2 = stack.pop(); //Boolean if(tmp2.type != Value.Type.BOOL){ throw new RuntimeError("Wrong Compare Operator types - Not BOOL"); } if(tmp1.type != Value.Type.BLOCK){ throw new RuntimeError("Wrong Compare Operator types - Not BLOCK"); } if(tmp2.truth == true){ interpret(tmp1.asTokenSource()); } }
public void __ifelse(){ //Takes 2 values. The second is a BOOL value. If the second one is true, then run the first //which might be a block Value tmp1a = stack.pop();//FALSE BLOCK Value tmp1b = stack.pop();//TRUE BLOCK Value tmp2 = stack.pop(); //Boolean if(tmp2.type != Value.Type.BOOL){ throw new RuntimeError("Wrong Compare Operator types - Not BOOL"); } if(tmp1b.type != Value.Type.BLOCK){ throw new RuntimeError("Wrong Compare Operator types - Not BLOCK"); } if(tmp1a.type != Value.Type.BLOCK){ throw new RuntimeError("Wrong Compare Operator types - Not BLOCK"); } if(tmp2.truth == true){ interpret(tmp1b.asTokenSource()); }else{ interpret(tmp1a.asTokenSource()); } }
public void __repeat(){ Value tmp2= stack.pop();//int Value tmp = stack.pop();//block BigInteger one = new BigInteger("1"); //THAT SUCKS System.out.println("Welcome to Repeat " + one ); BigInteger i = new BigInteger("0"); // System.out.println("Welcome to Repeat i=>" + i ); // System.out.println("Welcome to Repeat integer=> " + tmp.integer ); for(; tmp.integer.compareTo(i) > 0 ; i = i.add(one) ) { // System.out.println("Welcome to Repeat " + one ); // System.out.println("Welcome to Repeat"); interpret(tmp2.asTokenSource()); } }
public void __for(){ Value proc_block = stack.pop(); Value limit = stack.pop(); Value increment = stack.pop(); Value initial = stack.pop(); BigInteger control = initial.integer; //System.out.println("For : control: " + control ); //System.out.println("limit => " + limit.integer ); //System.out.println("increment => " + increment.integer ); //System.out.println("initial => " + initial.integer ); //System.out.println(control.compareTo(limit.integer)); while(control.compareTo(limit.integer) < 0){ stack.push(new Value(control)); //System.out.println("For : control: " + control ); //System.out.println("PROCBLOCK TYPE ==>" + proc_block.type ); //System.out.println("block procedure==>" + proc_block ); //System.out.println("Stack Top" + stack.peek() ); interpret(proc_block.asTokenSource()) ; //System.out.println("Stack Top" + stack.peek() ); control = control.add(increment.integer); } }
public void __while(){ Value ex = stack.pop(); System.out.println("ex block procedure==>" + ex ); Value bl = stack.pop(); System.out.println("bl block procedure==>" + bl ); interpret(bl.asTokenSource()); System.out.println("Stack Top => " + stack.peek() ); while(stack.pop().truth){ interpret(ex.asTokenSource()); interpret(bl.asTokenSource()); } }
private int __cmp(){ Value tmp1 = stack.pop(); Value tmp2 = stack.pop(); switch(tmp1.type) { case STR: case INT: case SYM: case BOOL: break; default: return -99; } switch (tmp2.type) { case STR: case INT: case SYM: case BOOL: break; default: return -99; } if(tmp2.type != tmp1.type){ return -1; } if(tmp1.type == Value.Type.INT){ return tmp2.integer.compareTo(tmp1.integer); } if( tmp1.type == Value.Type.STR ){ return tmp2.text.compareTo(tmp1.text); }
if( tmp1.type == Value.Type.SYM ){ if( tmp1.text.compareTo(tmp2.text) == 0 ){ return 0; }else{ return -1; } } if ( tmp1.type == Value.Type.BOOL){ if(tmp1.truth == false || tmp2.truth == false){ return -1; }else{ return 0; } } return -99; }
public void __pop(){ stack.pop(); }
public void __def() { Value v = stack.pop(); String id = stack.pop().asSymbol(); dict.put(id, v); }
public void __dup() { stack.push(stack.peek()); } public void __exch() { Value tmp = stack.pop(); Value tmp2 = stack.pop(); stack.push(tmp); stack.push(tmp2); }
public void __copy(){ Value elem = stack.pop(); ArrayList tmp_storage; tmp_storage = new ArrayList(); BigInteger top = elem.integer; BigInteger one = new BigInteger("1"); BigInteger i = new BigInteger("0"); while(top.compareTo(i) > 0 ){ tmp_storage.add(stack.pop()); i = i.add(one); } Collections.reverse(tmp_storage); Iterator itr = tmp_storage.iterator();
while(itr.hasNext()){ stack.push( itr.next() ); }
itr = tmp_storage.iterator(); while(itr.hasNext()){ stack.push( itr.next() ); } }
public void __index(){ ArrayList tmp_storage; tmp_storage = new ArrayList(); Value elem = stack.pop(); Value tmp_to_push_at_the_end = elem; BigInteger top = elem.integer; //index - need to add one BigInteger one = new BigInteger("1"); BigInteger i = new BigInteger("0"); top = top.add(one); while(top.compareTo(i) > 0 ){ tmp_to_push_at_the_end = stack.pop(); tmp_storage.add(tmp_to_push_at_the_end); i = i.add(one); } Collections.reverse(tmp_storage); Iterator itr = tmp_storage.iterator(); while(itr.hasNext()){ stack.push( itr.next() ); } stack.push(tmp_to_push_at_the_end); }
public void __clear(){ stack.clear(); }
public void __count(){ stack.push(new Value( BigInteger.valueOf(stack.size()) )); }
public static void main(String[] args) { System.out.println( "Patiently awaiting your code (^D or ^Z to end)"); Lexer lexer = new Lexer(new InputStreamReader(System.in)); Interpreter i = new Interpreter(); i.interpret(lexer); System.out.println(i.stack); } }
--------------030007050405090704000103 Content-Type: text/x-java; name="Lexer.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Lexer.java"
package pico;
import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader;
/** Tokenizer for Picoscript */ public class Lexer implements TokenSource {
/** We'll use the character representation of -1 to represent end-of-input * (the -1 actually becomes 65535 due to Java's UNSIGNED two-byte * characters, but that code point is uninhabited in Unicode so it's safe). */ static final char EOF = (char)-1;
Reader reader; // The input character stream char lookahead; // The single lookahead character int line = 1, column = 0; // Current position
/** Basic constructor uses the `Reader` type, so we can get one char at * a time. */ public Lexer(Reader reader) { this.reader = reader; consume(); }
/** Convenience constructor for tokenizing a string. */ public Lexer(String s) { this(new StringReader(s)); }
/** Discard the current lookahead character and load the next one. */ private void consume() { try { lookahead = (char)reader.read(); if(lookahead == '\n') { line++; column=0; } else { column++; } } catch(IOException exn) { lookahead = EOF; } }
public Token nextToken() { // TODO: this is the heart of your lexer while(lookahead != EOF) { Token t; int tokenLine = line; int tokenColumn = column; switch(lookahead) { case ' ': case '\t': case '\n': case '\r': consume(); continue; case '%': skipComment(); continue; case '{': consume(); t = new Token(Token.Type.LBRACE); break; case '}': consume(); t = new Token(Token.Type.RBRACE); break; case '/': consume(); t = new Token(Token.Type.SYM, scanIdentifier()); break; case '(': consume(); t = scanString(); break; default: if(Character.isDigit(lookahead) || lookahead == '-') { t = scanInteger(); } else if(Character.isAlphabetic(lookahead)) { t = new Token(Token.Type.OP, scanIdentifier()); } else { throw new LexError(line, column, "Invalid character: " + lookahead); } } if(t != null) return t.setPosition(tokenLine, tokenColumn); } return new Token(Token.Type.EOF); }
void skipComment() { do { consume(); } while(lookahead != '\n' && lookahead != EOF); }
Token scanInteger() { StringBuilder buffer = new StringBuilder(); do { buffer.append(lookahead); consume(); } while(Character.isDigit(lookahead)); return new Token(Token.Type.INT, buffer.toString()); }
static boolean isSeparator(char c) { switch(c) { case ' ': case '\n': case '\t': case '\r': case '{': case '}': case '(': case ')': case '%': case EOF: return true; default: return false; } }
String scanIdentifier() { StringBuilder buffer = new StringBuilder(); do { buffer.append(lookahead); consume(); } while(!isSeparator(lookahead)); return buffer.toString(); }
Token scanString() { StringBuilder buffer = new StringBuilder(); int depth = 1; while(true) { switch(lookahead) { case '(': depth++; break; case ')': depth--; if(depth == 0) { consume(); return new Token(Token.Type.STR, buffer.toString()); } break; case '\\': consume(); switch(lookahead) { case 'n': lookahead = '\n'; break; } break; case EOF: throw new LexError(line, column, "Unterminated string"); } buffer.append(lookahead); consume(); } }
/** Here is a sample program that reads an expression from standard * input. Press control-D (or maybe control-Z) to send the EOF and * print the tokens. */ public static void main(String[] args) { /* Read from standard input */ System.out.println( "Patiently awaiting your code (^D or ^Z to end)"); Lexer lexer = new Lexer(new InputStreamReader(System.in)); Token token; do { token = lexer.nextToken(); System.out.println(token); } while(token.type != Token.Type.EOF); }
}
--------------030007050405090704000103 Content-Type: text/x-java; name="LexError.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="LexError.java"
package pico;
public class LexError extends Error { public LexError(int line, int column, String message) { super(message + " -at-" + line + ":" + column); } } --------------030007050405090704000103 Content-Type: text/x-java; name="RuntimeError.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="RuntimeError.java"
package pico;
public class RuntimeError extends Error { public RuntimeError(String message) { super(message); }
public RuntimeError(Exception cause) { super(cause); } } --------------030007050405090704000103 Content-Type: text/x-java; name="Token.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Token.java"
package pico;
import org.junit.Assert;
import java.math.BigInteger; import java.util.List;
public class Token { //// TOKEN DATA Type type; // Required: what type of token is this? int line, column; // Optional: where is it in the source? String text; // Variant: only used for type in (STR,SYM,OP)
enum Type { EOF, // End of input LBRACE, // Left curly '{' RBRACE, // Right curly '}' INT, // Integer literal STR, // String literal SYM, // Unevaluated symbol OP, // Named operator, such as 'add' or 'dup' }
/** Basic constructor */ Token(Type type, String text) { this.type = type; this.text = text; }
/** Convenience constructor for when `text` can be null. */ Token(Type type) { this(type, null); }
/** Provide the position information */ public Token setPosition(int line, int column) { if(line <= 0) throw new IllegalArgumentException("Line# should be >0"); this.line = line; this.column = column; return this; }
public Token clearPosition() { line = column = 0; return this; }
/** We override toString so we can get sensible output when we try to * print tokens. */ -at-Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append(type); switch(type) { case INT: case STR: case SYM: case OP: // Half-hearted attempt to escape special chars // (it's only for debugging output) String escaped = text .replace("\n", "\\n") .replace("(", "\\(") .replace(")", "\\)"); buf.append('('); buf.append(escaped); buf.append(')'); break; } if(line > 0) { buf.append("-at-" + line + ":" + column); } return buf.toString(); }
/** Just a short program to illustrate some of the capabilities of Tokens. */ public static void main(String[] args) { Token t1 = new Token(Type.SYM, "square"); showEqual(t1, "SYM(square)");
Token t2 = new Token(Type.INT, "298347509328760987"); showEqual(t2, "INT(298347509328760987)");
Token t3 = new Token(Type.LBRACE).setPosition(2,5); showEqual(t3, "LBRACE-at-2:5");
Token t4 = new Token(Type.OP, "dup"); showEqual(t4, "OP(dup)");
Token t5 = new Token(Type.STR, "hello world!").setPosition(15,8); showEqual(t5, "STR(hello world!)-at-15:8"); }
static void showEqual(Token t, String expected) { String s = t.toString(); System.out.println(s); Assert.assertEquals(expected, s); } } --------------030007050405090704000103 Content-Type: text/x-java; name="TokenListSource.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="TokenListSource.java"
package pico;
import java.util.ArrayList;
public class TokenListSource implements TokenSource { ArrayList list; int nextIndex = 0;
public TokenListSource(ArrayList list) { this.list = list; }
-at-Override public Token nextToken() { if(nextIndex < list.size()) { return list.get(nextIndex++); } else { return new Token(Token.Type.EOF); } } }
--------------030007050405090704000103 Content-Type: text/x-java; name="TokenSource.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="TokenSource.java"
package pico;
public interface TokenSource { Token nextToken(); }
--------------030007050405090704000103 Content-Type: text/x-java; name="Value.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Value.java"
package pico;
import java.math.BigInteger; import java.util.ArrayList;
public class Value {
enum Type { INT, SYM, STR, BLOCK, BOOL };
Type type; String text; // used for SYM, STR BigInteger integer; // used for INT ArrayList list; // used for BLOCK boolean truth; //used for BOOL
public Value(BigInteger integer) { type = Type.INT; this.integer = integer; } public Value(boolean b){ type = Type.BOOL; this.truth = new Boolean(b); }
public Value(TokenSource tokens) { type = Type.BLOCK; list = new ArrayList<>(); int depth = 1; while(true) { Token t = tokens.nextToken(); if(t.type == Token.Type.RBRACE) { depth--; if(depth == 0) return; } else if(t.type == Token.Type.LBRACE) { depth++; } list.add(t); } }
public Value(Token token) { switch(token.type) { case INT: type = Type.INT; integer = new BigInteger(token.text); break; case SYM: type = Type.SYM; text = token.text; break; case STR: type = Type.STR; text = token.text; break; default: throw new RuntimeError("Token cannot become a value: " + token); } }
public BigInteger asInteger() { if(type == Type.INT) { return integer; } else { throw new RuntimeError( "Expected INT, found " + type ); } }
public TokenSource asTokenSource() { if(type == Type.BLOCK) { return new TokenListSource(list); } else { throw new RuntimeError( "Expected BLOCK, found " + type ); } }
public String asSymbol() { if(type == Type.SYM) { return text; } else { throw new RuntimeError( "Expected SYM, found " + type ); } }
-at-Override public String toString() { switch(type) { case INT: return integer.toString(); case SYM: case STR: return text; case BLOCK: return list.toString(); case BOOL: return Boolean.toString(truth); default: throw new RuntimeError("Unhandled type: "+type); } }
public static void main(String[] args) {
Token t1 = new Token(Token.Type.SYM, "foo"); Value v1 = new Value(t1); System.out.println(t1); System.out.println(v1); } }
--------------030007050405090704000103 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ hangout mailing list hangout-at-nylxs.com http://www.nylxs.com/ --------------030007050405090704000103--
|
|