Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java @ 14852:f49e2f9cbdc3
Merge (defunc).
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Thu, 27 Mar 2014 15:06:22 +0100 |
parents | 89ac75425681 |
children | 8de99b84c9cd |
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:
13805
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 */ |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
23 package com.oracle.truffle.sl.parser; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
24 |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
25 import java.math.*; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
26 import java.util.*; |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
27 |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
28 import com.oracle.truffle.api.*; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
29 import com.oracle.truffle.api.frame.*; |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
30 import com.oracle.truffle.api.impl.*; |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
31 import com.oracle.truffle.api.nodes.*; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
32 import com.oracle.truffle.sl.nodes.*; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
33 import com.oracle.truffle.sl.nodes.call.*; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
34 import com.oracle.truffle.sl.nodes.controlflow.*; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
35 import com.oracle.truffle.sl.nodes.expression.*; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
36 import com.oracle.truffle.sl.nodes.local.*; |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
37 import com.oracle.truffle.sl.runtime.*; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
38 |
13836
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
39 /** |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
40 * Helper class used by the SL {@link Parser} to create nodes. The code is factored out of the |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
41 * automatically generated parser to keep the attributed grammar of SL small. |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
42 */ |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
43 public class SLNodeFactory { |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
44 |
13836
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
45 /** |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
46 * Local variable names that are visible in the current block. Variables are not visible outside |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
47 * of their defining block, to prevent the usage of undefined variables. Because of that, we can |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
48 * decide during parsing if a name references a local variable or is a function name. |
64c77f0577bb
More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13821
diff
changeset
|
49 */ |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
50 static class LexicalScope { |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
51 protected final LexicalScope outer; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
52 protected final Map<String, FrameSlot> locals; |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
53 |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
54 public LexicalScope(LexicalScope outer) { |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
55 this.outer = outer; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
56 this.locals = new HashMap<>(); |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
57 if (outer != null) { |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
58 locals.putAll(outer.locals); |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
59 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
60 } |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
61 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
62 |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
63 /* State while parsing a source unit. */ |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
64 private final SLContext context; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
65 private final Source source; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
66 |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
67 /* State while parsing a function. */ |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
68 private String functionName; |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
69 private int parameterCount; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
70 private FrameDescriptor frameDescriptor; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
71 private List<SLStatementNode> methodNodes; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
72 |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
73 /* State while parsing a block. */ |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
74 private LexicalScope lexicalScope; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
75 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
76 public SLNodeFactory(SLContext context, Source source) { |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
77 this.context = context; |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
78 this.source = source; |
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
79 } |
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
80 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
81 public void startFunction(Token nameToken) { |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
82 assert functionName == null; |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
83 assert parameterCount == 0; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
84 assert frameDescriptor == null; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
85 assert lexicalScope == null; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
86 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
87 functionName = nameToken.val; |
9258
07f8d136a05e
Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents:
9222
diff
changeset
|
88 frameDescriptor = new FrameDescriptor(); |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
89 methodNodes = new ArrayList<>(); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
90 startBlock(); |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
91 } |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
92 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
93 public void addFormalParameter(Token nameToken) { |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
94 /* |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
95 * Method parameters are assigned to local variables at the beginning of the method. This |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
96 * ensures that accesses to parameters are specialized the same way as local variables are |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
97 * specialized. |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
98 */ |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
99 SLReadArgumentNode readArg = assignSource(nameToken, new SLReadArgumentNode(parameterCount)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
100 methodNodes.add(createAssignment(nameToken, readArg)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
101 parameterCount++; |
7292
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 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
104 public void finishFunction(SLStatementNode bodyNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
105 methodNodes.add(bodyNode); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
106 SLStatementNode methodBlock = finishBlock(methodNodes); |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
107 assert lexicalScope == null : "Wrong scoping of blocks in parser"; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
108 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
109 SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(methodBlock); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
110 SLRootNode rootNode = new SLRootNode(frameDescriptor, functionBodyNode, functionName); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
111 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
112 context.getFunctionRegistry().register(functionName, rootNode); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
113 |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
114 functionName = null; |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
115 parameterCount = 0; |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
116 frameDescriptor = null; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
117 lexicalScope = null; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
118 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
119 |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
120 public void startBlock() { |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
121 lexicalScope = new LexicalScope(lexicalScope); |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
122 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
123 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
124 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes) { |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
125 lexicalScope = lexicalScope.outer; |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
126 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
127 List<SLStatementNode> flattenedNodes = new ArrayList<>(bodyNodes.size()); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
128 flattenBlocks(bodyNodes, flattenedNodes); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
129 if (flattenedNodes.size() == 1) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
130 /* A block containing one other node is unnecessary, we can just that other node. */ |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
131 return flattenedNodes.get(0); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
132 } else { |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
133 return new SLBlockNode(flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()])); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
134 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
135 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
136 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
137 private void flattenBlocks(Iterable<? extends Node> bodyNodes, List<SLStatementNode> flattenedNodes) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
138 for (Node n : bodyNodes) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
139 if (n instanceof SLBlockNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
140 flattenBlocks(n.getChildren(), flattenedNodes); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
141 } else { |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
142 flattenedNodes.add((SLStatementNode) n); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
143 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
144 } |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
145 } |
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
146 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
147 public SLStatementNode createBreak(Token t) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
148 return assignSource(t, new SLBreakNode()); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
149 } |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
150 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
151 public SLStatementNode createContinue(Token t) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
152 return assignSource(t, new SLContinueNode()); |
7292
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 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
155 public SLStatementNode createWhile(Token t, SLExpressionNode conditionNode, SLStatementNode bodyNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
156 return assignSource(t, new SLWhileNode(conditionNode, bodyNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
157 } |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
158 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
159 public SLStatementNode createIf(Token t, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
160 return assignSource(t, new SLIfNode(conditionNode, thenPartNode, elsePartNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
161 } |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
162 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
163 public SLStatementNode createReturn(Token t, SLExpressionNode valueNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
164 return assignSource(t, new SLReturnNode(valueNode)); |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
165 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
166 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
167 public SLExpressionNode createBinary(Token opToken, SLExpressionNode leftNode, SLExpressionNode rightNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
168 switch (opToken.val) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
169 case "+": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
170 return assignSource(opToken, SLAddNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
171 case "*": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
172 return assignSource(opToken, SLMulNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
173 case "/": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
174 return assignSource(opToken, SLDivNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
175 case "-": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
176 return assignSource(opToken, SLSubNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
177 case "<": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
178 return assignSource(opToken, SLLessThanNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
179 case "<=": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
180 return assignSource(opToken, SLLessOrEqualNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
181 case ">": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
182 return assignSource(opToken, SLLogicalNotNodeFactory.create(assignSource(opToken, SLLessOrEqualNodeFactory.create(leftNode, rightNode)))); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
183 case ">=": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
184 return assignSource(opToken, SLLogicalNotNodeFactory.create(assignSource(opToken, SLLessThanNodeFactory.create(leftNode, rightNode)))); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
185 case "==": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
186 return assignSource(opToken, SLEqualNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
187 case "!=": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
188 return assignSource(opToken, SLLogicalNotNodeFactory.create(assignSource(opToken, SLEqualNodeFactory.create(leftNode, rightNode)))); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
189 case "&&": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
190 return assignSource(opToken, SLLogicalAndNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
191 case "||": |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
192 return assignSource(opToken, SLLogicalOrNodeFactory.create(leftNode, rightNode)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
193 default: |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
194 throw new RuntimeException("unexpected operation: " + opToken.val); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
195 } |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
196 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
197 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
198 public SLExpressionNode createCall(Token nameToken, List<SLExpressionNode> parameterNodes) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
199 SLExpressionNode functionNode = createRead(nameToken); |
13943
89ac75425681
SL: small cleanups
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13836
diff
changeset
|
200 return assignSource(nameToken, SLInvokeNode.create(functionNode, parameterNodes.toArray(new SLExpressionNode[parameterNodes.size()]))); |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
201 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
202 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
203 public SLExpressionNode createAssignment(Token nameToken, SLExpressionNode valueNode) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
204 FrameSlot frameSlot = frameDescriptor.findOrAddFrameSlot(nameToken.val); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
205 lexicalScope.locals.put(nameToken.val, frameSlot); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
206 return assignSource(nameToken, SLWriteLocalVariableNodeFactory.create(valueNode, frameSlot)); |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
207 } |
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
208 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
209 public SLExpressionNode createRead(Token nameToken) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
210 FrameSlot frameSlot = lexicalScope.locals.get(nameToken.val); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
211 if (frameSlot != null) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
212 /* Read of a local variable. */ |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
213 return assignSource(nameToken, SLReadLocalVariableNodeFactory.create(frameSlot)); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
214 } else { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
215 /* Read of a global name. In our language, the only global names are functions. */ |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
216 return assignSource(nameToken, new SLFunctionLiteralNode(context.getFunctionRegistry().lookup(nameToken.val))); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
217 } |
13761
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
218 } |
7c418666c6c9
Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13276
diff
changeset
|
219 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
220 public SLExpressionNode createStringLiteral(Token literalToken) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
221 /* Remove the trailing and ending " */ |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
222 String literal = literalToken.val; |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
223 assert literal.length() >= 2 && literal.startsWith("\"") && literal.endsWith("\""); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
224 literal = literal.substring(1, literal.length() - 1); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
225 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
226 return assignSource(literalToken, new SLStringLiteralNode(literal)); |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
227 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
228 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
229 public SLExpressionNode createNumericLiteral(Token literalToken) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
230 try { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
231 /* Try if the literal is small enough to fit into a long value. */ |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
232 return assignSource(literalToken, new SLLongLiteralNode(Long.parseLong(literalToken.val))); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
233 } catch (NumberFormatException ex) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
234 /* Overflow of long value, so fall back to BigInteger. */ |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
235 return assignSource(literalToken, new SLBigIntegerLiteralNode(new BigInteger(literalToken.val))); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
236 } |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
237 } |
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
238 |
13821
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
239 private <T extends Node> T assignSource(Token t, T node) { |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
240 assert functionName != null; |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
241 assert t != null; |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
242 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
243 int startLine = t.line; |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
244 int startColumn = t.col; |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
245 int charLength = t.val.length(); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
246 SourceSection sourceSection = new DefaultSourceSection(source, functionName, startLine, startColumn, 0, charLength); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
247 |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
248 node.assignSourceSection(sourceSection); |
b16ec83edc73
Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents:
13805
diff
changeset
|
249 return node; |
12752
71991b7a0f14
SL: Enhanced SimpleLanguage with support for if statements, function calls, function caching + inlining and builtins.
Christian Humer <christian.humer@gmail.com>
parents:
12390
diff
changeset
|
250 } |
7292
213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff
changeset
|
251 } |