Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/Scanner.java @ 21646:645f170013a4
Fix in the scheduler for proxy node handling.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Mon, 01 Jun 2015 22:14:34 +0200 |
parents | b1530a6cce8c |
children |
rev | line source |
---|---|
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
1 /* |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
4 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
8 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
14 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
18 * |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
21 * questions. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
22 */ |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
23 |
13763
cad8bed1a1ac
Fix Eclipse formatter issues
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13761
diff
changeset
|
24 // The content of this file is automatically generated. DO NOT EDIT. |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
25 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
26 package com.oracle.truffle.sl.parser; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
27 |
21554
b1530a6cce8c
renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
18411
diff
changeset
|
28 import java.io.*; |
b1530a6cce8c
renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
18411
diff
changeset
|
29 import java.util.*; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
30 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
31 // Checkstyle: stop |
7517
b6743d7eb8d4
disabled auto-formatting of generated code
Doug Simon <doug.simon@oracle.com>
parents:
7292
diff
changeset
|
32 // @formatter:off |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
33 class Token { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
34 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
35 public int kind; // token kind |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
36 public int pos; // token position in bytes in the source text (starting at 0) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
37 public int charPos; // token position in characters in the source text (starting at 0) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
38 public int col; // token column (starting at 1) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
39 public int line; // token line (starting at 1) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
40 public String val; // token value |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
41 public Token next; // ML 2005-03-11 Peek tokens are kept in linked list |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
42 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
43 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
44 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
45 // Buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
46 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
47 class Buffer { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
48 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
49 // This Buffer supports the following cases: |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
50 // 1) seekable stream (file) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
51 // a) whole stream in buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
52 // b) part of stream in buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
53 // 2) non seekable stream (network, console) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
54 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
55 public static final int EOF = Character.MAX_VALUE + 1; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
56 private static final int MIN_BUFFER_LENGTH = 1024; // 1KB |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
57 private static final int MAX_BUFFER_LENGTH = MIN_BUFFER_LENGTH * 64; // 64KB |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
58 private byte[] buf; // input buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
59 private int bufStart; // position of first byte in buffer relative to input stream |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
60 private int bufLen; // length of buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
61 private int fileLen; // length of input stream (may change if stream is no file) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
62 private int bufPos; // current position in buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
63 private RandomAccessFile file; // input stream (seekable) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
64 private InputStream stream; // growing input stream (e.g.: console, network) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
65 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
66 public Buffer(InputStream s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
67 stream = s; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
68 fileLen = bufLen = bufStart = bufPos = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
69 buf = new byte[MIN_BUFFER_LENGTH]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
70 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
71 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
72 public Buffer(String fileName) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
73 try { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
74 file = new RandomAccessFile(fileName, "r"); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
75 fileLen = (int) file.length(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
76 bufLen = Math.min(fileLen, MAX_BUFFER_LENGTH); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
77 buf = new byte[bufLen]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
78 bufStart = Integer.MAX_VALUE; // nothing in buffer so far |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
79 if (fileLen > 0) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
80 setPos(0); // setup buffer to position 0 (start) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
81 else |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
82 bufPos = 0; // index 0 is already after the file, thus setPos(0) is invalid |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
83 if (bufLen == fileLen) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
84 Close(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
85 } catch (IOException e) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
86 throw new FatalError("Could not open file " + fileName); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
87 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
88 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
89 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
90 // don't use b after this call anymore |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
91 // called in UTF8Buffer constructor |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
92 protected Buffer(Buffer b) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
93 buf = b.buf; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
94 bufStart = b.bufStart; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
95 bufLen = b.bufLen; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
96 fileLen = b.fileLen; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
97 bufPos = b.bufPos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
98 file = b.file; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
99 stream = b.stream; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
100 // keep finalize from closing the file |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
101 b.file = null; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
102 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
103 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
104 @Override |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
105 protected void finalize() throws Throwable { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
106 super.finalize(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
107 Close(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
108 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
109 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
110 protected void Close() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
111 if (file != null) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
112 try { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
113 file.close(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
114 file = null; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
115 } catch (IOException e) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
116 throw new FatalError(e.getMessage()); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
117 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
118 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
119 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
120 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
121 public int Read() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
122 if (bufPos < bufLen) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
123 return buf[bufPos++] & 0xff; // mask out sign bits |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
124 } else if (getPos() < fileLen) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
125 setPos(getPos()); // shift buffer start to pos |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
126 return buf[bufPos++] & 0xff; // mask out sign bits |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
127 } else if (stream != null && ReadNextStreamChunk() > 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
128 return buf[bufPos++] & 0xff; // mask out sign bits |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
129 } else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
130 return EOF; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
131 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
132 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
133 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
134 public int Peek() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
135 int curPos = getPos(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
136 int ch = Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
137 setPos(curPos); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
138 return ch; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
139 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
140 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
141 // beg .. begin, zero-based, inclusive, in byte |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
142 // end .. end, zero-based, exclusive, in byte |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
143 public String GetString(int beg, int end) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
144 int len = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
145 char[] buffer = new char[end - beg]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
146 int oldPos = getPos(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
147 setPos(beg); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
148 while (getPos() < end) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
149 buffer[len++] = (char) Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
150 setPos(oldPos); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
151 return new String(buffer, 0, len); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
152 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
153 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
154 public int getPos() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
155 return bufPos + bufStart; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
156 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
157 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
158 public void setPos(int value) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
159 if (value >= fileLen && stream != null) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
160 // Wanted position is after buffer and the stream |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
161 // is not seek-able e.g. network or console, |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
162 // thus we have to read the stream manually till |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
163 // the wanted position is in sight. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
164 while (value >= fileLen && ReadNextStreamChunk() > 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
165 // nothing to do... |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
166 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
167 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
168 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
169 if (value < 0 || value > fileLen) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
170 throw new FatalError("buffer out of bounds access, position: " + value); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
171 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
172 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
173 if (value >= bufStart && value < bufStart + bufLen) { // already in buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
174 bufPos = value - bufStart; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
175 } else if (file != null) { // must be swapped in |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
176 try { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
177 file.seek(value); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
178 bufLen = file.read(buf); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
179 bufStart = value; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
180 bufPos = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
181 } catch (IOException e) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
182 throw new FatalError(e.getMessage()); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
183 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
184 } else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
185 // set the position to the end of the file, Pos will return fileLen. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
186 bufPos = fileLen - bufStart; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
187 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
188 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
189 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
190 // Read the next chunk of bytes from the stream, increases the buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
191 // if needed and updates the fields fileLen and bufLen. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
192 // Returns the number of bytes read. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
193 private int ReadNextStreamChunk() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
194 int free = buf.length - bufLen; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
195 if (free == 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
196 // in the case of a growing input stream |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
197 // we can neither seek in the stream, nor can we |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
198 // foresee the maximum length, thus we must adapt |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
199 // the buffer size on demand. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
200 byte[] newBuf = new byte[bufLen * 2]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
201 System.arraycopy(buf, 0, newBuf, 0, bufLen); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
202 buf = newBuf; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
203 free = bufLen; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
204 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
205 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
206 int read; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
207 try { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
208 read = stream.read(buf, bufLen, free); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
209 } catch (IOException ioex) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
210 throw new FatalError(ioex.getMessage()); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
211 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
212 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
213 if (read > 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
214 fileLen = bufLen = (bufLen + read); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
215 return read; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
216 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
217 // end of stream reached |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
218 return 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
219 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
220 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
221 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
222 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
223 // UTF8Buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
224 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
225 class UTF8Buffer extends Buffer { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
226 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
227 UTF8Buffer(Buffer b) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
228 super(b); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
229 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
230 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
231 @Override |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
232 public int Read() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
233 int ch; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
234 do { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
235 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
236 // until we find a utf8 start (0xxxxxxx or 11xxxxxx) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
237 } while ((ch >= 128) && ((ch & 0xC0) != 0xC0) && (ch != EOF)); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
238 if (ch < 128 || ch == EOF) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
239 // nothing to do, first 127 chars are the same in ascii and utf8 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
240 // 0xxxxxxx or end of file character |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
241 } else if ((ch & 0xF0) == 0xF0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
242 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
243 int c1 = ch & 0x07; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
244 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
245 int c2 = ch & 0x3F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
246 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
247 int c3 = ch & 0x3F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
248 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
249 int c4 = ch & 0x3F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
250 ch = (((((c1 << 6) | c2) << 6) | c3) << 6) | c4; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
251 } else if ((ch & 0xE0) == 0xE0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
252 // 1110xxxx 10xxxxxx 10xxxxxx |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
253 int c1 = ch & 0x0F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
254 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
255 int c2 = ch & 0x3F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
256 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
257 int c3 = ch & 0x3F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
258 ch = (((c1 << 6) | c2) << 6) | c3; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
259 } else if ((ch & 0xC0) == 0xC0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
260 // 110xxxxx 10xxxxxx |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
261 int c1 = ch & 0x1F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
262 ch = super.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
263 int c2 = ch & 0x3F; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
264 ch = (c1 << 6) | c2; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
265 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
266 return ch; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
267 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
268 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
269 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
270 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
271 // StartStates -- maps characters to start states of tokens |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
272 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
273 class StartStates { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
274 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
275 private static class Elem { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
276 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
277 public int key, val; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
278 public Elem next; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
279 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
280 public Elem(int key, int val) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
281 this.key = key; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
282 this.val = val; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
283 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
284 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
285 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
286 private Elem[] tab = new Elem[128]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
287 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
288 public void set(int key, int val) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
289 Elem e = new Elem(key, val); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
290 int k = key % 128; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
291 e.next = tab[k]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
292 tab[k] = e; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
293 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
294 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
295 public int state(int key) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
296 Elem e = tab[key % 128]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
297 while (e != null && e.key != key) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
298 e = e.next; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
299 return e == null ? 0 : e.val; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
300 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
301 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
302 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
303 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
304 // Scanner |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
305 // ----------------------------------------------------------------------------------- |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
306 @SuppressWarnings({"rawtypes", "unchecked"}) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
307 public class Scanner { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
308 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
309 static final char EOL = '\n'; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
310 static final int eofSym = 0; |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
311 static final int maxT = 31; |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
312 static final int noSym = 31; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
313 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
314 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
315 public Buffer buffer; // scanner buffer |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
316 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
317 Token t; // current token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
318 int ch; // current input character |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
319 int pos; // byte position of current character |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
320 int charPos; // position by unicode characters starting with 0 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
321 int col; // column number of current character |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
322 int line; // line number of current character |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
323 int oldEols; // EOLs that appeared in a comment; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
324 static final StartStates start; // maps initial token character to start state |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
325 static final Map literals; // maps literal strings to literal kinds |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
326 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
327 Token tokens; // list of tokens already peeked (first token is a dummy) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
328 Token pt; // current peek token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
329 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
330 char[] tval = new char[16]; // token text used in NextToken(), dynamically enlarged |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
331 int tlen; // length of current token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
332 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
333 static { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
334 start = new StartStates(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
335 literals = new HashMap(); |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
336 for (int i = 65; i <= 90; ++i) start.set(i, 1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
337 for (int i = 97; i <= 122; ++i) start.set(i, 1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
338 for (int i = 49; i <= 57; ++i) start.set(i, 4); |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
339 start.set(34, 2); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
340 start.set(48, 5); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
341 start.set(40, 6); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
342 start.set(44, 7); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
343 start.set(41, 8); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
344 start.set(123, 9); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
345 start.set(125, 10); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
346 start.set(59, 11); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
347 start.set(124, 12); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
348 start.set(38, 14); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
349 start.set(60, 26); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
350 start.set(62, 27); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
351 start.set(61, 28); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
352 start.set(33, 19); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
353 start.set(43, 21); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
354 start.set(45, 22); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
355 start.set(42, 23); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
356 start.set(47, 24); |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
357 start.set(46, 25); |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
358 start.set(Buffer.EOF, -1); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
359 literals.put("function", new Integer(4)); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
360 literals.put("break", new Integer(10)); |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
361 literals.put("continue", new Integer(12)); |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
362 literals.put("while", new Integer(13)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
363 literals.put("if", new Integer(14)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
364 literals.put("else", new Integer(15)); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
365 literals.put("return", new Integer(16)); |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
366 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
367 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
368 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
369 public Scanner(String fileName) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
370 buffer = new Buffer(fileName); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
371 Init(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
372 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
373 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
374 public Scanner(InputStream s) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
375 buffer = new Buffer(s); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
376 Init(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
377 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
378 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
379 void Init() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
380 pos = -1; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
381 line = 1; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
382 col = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
383 charPos = -1; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
384 oldEols = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
385 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
386 if (ch == 0xEF) { // check optional byte order mark for UTF-8 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
387 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
388 int ch1 = ch; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
389 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
390 int ch2 = ch; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
391 if (ch1 != 0xBB || ch2 != 0xBF) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
392 throw new FatalError("Illegal byte order mark at start of file"); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
393 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
394 buffer = new UTF8Buffer(buffer); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
395 col = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
396 charPos = -1; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
397 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
398 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
399 pt = tokens = new Token(); // first token is a dummy |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
400 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
401 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
402 void NextCh() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
403 if (oldEols > 0) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
404 ch = EOL; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
405 oldEols--; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
406 } else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
407 pos = buffer.getPos(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
408 // buffer reads unicode chars, if UTF8 has been detected |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
409 ch = buffer.Read(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
410 col++; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
411 charPos++; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
412 // replace isolated '\r' by '\n' in order to make |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
413 // eol handling uniform across Windows, Unix and Mac |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
414 if (ch == '\r' && buffer.Peek() != '\n') |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
415 ch = EOL; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
416 if (ch == EOL) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
417 line++; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
418 col = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
419 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
420 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
421 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
422 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
423 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
424 void AddCh() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
425 if (tlen >= tval.length) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
426 char[] newBuf = new char[2 * tval.length]; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
427 System.arraycopy(tval, 0, newBuf, 0, tval.length); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
428 tval = newBuf; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
429 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
430 if (ch != Buffer.EOF) { |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
431 tval[tlen++] = (char)ch; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
432 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
433 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
434 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
435 } |
7517
b6743d7eb8d4
disabled auto-formatting of generated code
Doug Simon <doug.simon@oracle.com>
parents:
7292
diff
changeset
|
436 |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
437 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
438 boolean Comment0() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
439 int level = 1, pos0 = pos, line0 = line, col0 = col, charPos0 = charPos; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
440 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
441 if (ch == '/') { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
442 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
443 for(;;) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
444 if (ch == 10) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
445 level--; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
446 if (level == 0) { oldEols = line - line0; NextCh(); return true; } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
447 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
448 } else if (ch == Buffer.EOF) return false; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
449 else NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
450 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
451 } else { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
452 buffer.setPos(pos0); NextCh(); line = line0; col = col0; charPos = charPos0; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
453 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
454 return false; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
455 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
456 |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
457 boolean Comment1() { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
458 int level = 1, pos0 = pos, line0 = line, col0 = col, charPos0 = charPos; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
459 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
460 if (ch == '*') { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
461 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
462 for(;;) { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
463 if (ch == '*') { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
464 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
465 if (ch == '/') { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
466 level--; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
467 if (level == 0) { oldEols = line - line0; NextCh(); return true; } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
468 NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
469 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
470 } else if (ch == Buffer.EOF) return false; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
471 else NextCh(); |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
472 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
473 } else { |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
474 buffer.setPos(pos0); NextCh(); line = line0; col = col0; charPos = charPos0; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
475 } |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
476 return false; |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
477 } |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
478 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
479 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
480 void CheckLiteral() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
481 String val = t.val; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
482 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
483 Object kind = literals.get(val); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
484 if (kind != null) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
485 t.kind = ((Integer) kind).intValue(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
486 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
487 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
488 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
489 Token NextToken() { |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
490 while (ch == ' ' || |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
491 ch >= 9 && ch <= 10 || ch == 13 |
7848
698cd036a1ca
Added a ternary operator to simple language to test short circuit specialization.
Christian Humer <christian.humer@gmail.com>
parents:
7530
diff
changeset
|
492 ) NextCh(); |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
493 if (ch == '/' && Comment0() ||ch == '/' && Comment1()) return NextToken(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
494 int recKind = noSym; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
495 int recEnd = pos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
496 t = new Token(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
497 t.pos = pos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
498 t.col = col; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
499 t.line = line; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
500 t.charPos = charPos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
501 int state = start.state(ch); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
502 tlen = 0; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
503 AddCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
504 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
505 loop: for (;;) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
506 switch (state) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
507 case -1: { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
508 t.kind = eofSym; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
509 break loop; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
510 } // NextCh already done |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
511 case 0: { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
512 if (recKind != noSym) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
513 tlen = recEnd - t.pos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
514 SetScannerBehindT(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
515 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
516 t.kind = recKind; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
517 break loop; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
518 } // NextCh already done |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
519 case 1: |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
520 recEnd = pos; recKind = 1; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
521 if (ch >= '0' && ch <= '9' || ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {AddCh(); state = 1; break;} |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
522 else {t.kind = 1; t.val = new String(tval, 0, tlen); CheckLiteral(); return t;} |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
523 case 2: |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
524 if (ch <= 9 || ch >= 11 && ch <= 12 || ch >= 14 && ch <= '!' || ch >= '#' && ch <= '[' || ch >= ']' && ch <= 65535) {AddCh(); state = 2; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
525 else if (ch == '"') {AddCh(); state = 3; break;} |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
526 else {state = 0; break;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
527 case 3: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
528 {t.kind = 2; break loop;} |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
529 case 4: |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
530 recEnd = pos; recKind = 3; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
531 if (ch >= '0' && ch <= '9') {AddCh(); state = 4; break;} |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
532 else {t.kind = 3; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
533 case 5: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
534 {t.kind = 3; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
535 case 6: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
536 {t.kind = 5; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
537 case 7: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
538 {t.kind = 6; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
539 case 8: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
540 {t.kind = 7; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
541 case 9: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
542 {t.kind = 8; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
543 case 10: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
544 {t.kind = 9; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
545 case 11: |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
546 {t.kind = 11; break loop;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
547 case 12: |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
548 if (ch == '|') {AddCh(); state = 13; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
549 else {state = 0; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
550 case 13: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
551 {t.kind = 17; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
552 case 14: |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
553 if (ch == '&') {AddCh(); state = 15; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
554 else {state = 0; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
555 case 15: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
556 {t.kind = 18; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
557 case 16: |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
558 {t.kind = 20; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
559 case 17: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
560 {t.kind = 22; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
561 case 18: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
562 {t.kind = 23; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
563 case 19: |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
564 if (ch == '=') {AddCh(); state = 20; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
565 else {state = 0; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
566 case 20: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
567 {t.kind = 24; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
568 case 21: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
569 {t.kind = 25; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
570 case 22: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
571 {t.kind = 26; break loop;} |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
572 case 23: |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
573 {t.kind = 27; break loop;} |
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
574 case 24: |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
575 {t.kind = 28; break loop;} |
12754
7311354f5bf8
SL: fixed eclipse formatter problems.
Christian Humer <christian.humer@gmail.com>
parents:
12752
diff
changeset
|
576 case 25: |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
577 {t.kind = 30; break loop;} |
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
578 case 26: |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
579 recEnd = pos; recKind = 19; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
580 if (ch == '=') {AddCh(); state = 16; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
581 else {t.kind = 19; break loop;} |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
582 case 27: |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
583 recEnd = pos; recKind = 21; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
584 if (ch == '=') {AddCh(); state = 17; break;} |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13763
diff
changeset
|
585 else {t.kind = 21; break loop;} |
18411
dc2e000bed40
SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents:
16512
diff
changeset
|
586 case 28: |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
587 recEnd = pos; recKind = 29; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
588 if (ch == '=') {AddCh(); state = 18; break;} |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
12754
diff
changeset
|
589 else {t.kind = 29; break loop;} |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
590 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
591 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
592 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
593 t.val = new String(tval, 0, tlen); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
594 return t; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
595 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
596 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
597 private void SetScannerBehindT() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
598 buffer.setPos(t.pos); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
599 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
600 line = t.line; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
601 col = t.col; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
602 charPos = t.charPos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
603 for (int i = 0; i < tlen; i++) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
604 NextCh(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
605 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
606 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
607 // get the next token (possibly a token already seen during peeking) |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
608 public Token Scan() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
609 if (tokens.next == null) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
610 return NextToken(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
611 } else { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
612 pt = tokens = tokens.next; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
613 return tokens; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
614 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
615 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
616 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
617 // get the next token, ignore pragmas |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
618 public Token Peek() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
619 do { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
620 if (pt.next == null) { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
621 pt.next = NextToken(); |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
622 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
623 pt = pt.next; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
624 } while (pt.kind > maxT); // skip pragmas |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
625 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
626 return pt; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
627 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
628 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
629 // make sure that peeking starts at current scan position |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
630 public void ResetPeek() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
631 pt = tokens; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
632 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
633 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
634 // The following methods are used for the CLNG Editor and will be called with java.Reflection. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
635 // If the editor won't be used these 3 functions are obsolete, |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
636 // otherwise changes within the signature of the methods will result in Syntax Highlighting not working properly |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
637 // anymore. |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
638 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
639 // get the offset of the next Token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
640 public int getPeekTokenOffset() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
641 return pt.pos; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
642 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
643 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
644 // get the String value of the Token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
645 public String getPeekTokenVal() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
646 return pt.val; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
647 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
648 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
649 // get the Kind value of the Token |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
650 public int getPeekTokenKind() { |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
651 return pt.kind; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
652 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
653 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
654 } // end Scanner |