annotate truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java @ 22548:6b76a24fffbd default tip

Use all variables: a, b, u,v and x, y
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Thu, 14 Jan 2016 14:20:57 +0100
parents c1f804ce6cad
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
5 * The Universal Permissive License (UPL), Version 1.0
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
6 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
7 * Subject to the condition set forth below, permission is hereby granted to any
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
8 * person obtaining a copy of this software, associated documentation and/or
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
9 * data (collectively the "Software"), free of charge and under any and all
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
10 * copyright rights in the Software, and any and all patent rights owned or
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
11 * freely licensable by each licensor hereunder covering either (i) the
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
12 * unmodified Software as contributed to or provided by such licensor, or (ii)
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
13 * the Larger Works (as defined below), to deal in both
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
14 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
15 * (a) the Software, and
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
16 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
17 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
18 * one is included with the Software each a "Larger Work" to which the Software
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
19 * is contributed by such licensors),
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
20 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
21 * without restriction, including without limitation the rights to copy, create
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
22 * derivative works of, display, perform, and distribute the Software and make,
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
23 * use, sell, offer for sale, import, export, have made, and have sold the
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
24 * Software and the Larger Work(s), and to sublicense the foregoing rights on
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
25 * either these or other terms.
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
26 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
27 * This license is subject to the following condition:
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
28 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
29 * The above copyright notice and either this complete permission notice or at a
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
30 * minimum a reference to the UPL must be included in all copies or substantial
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
31 * portions of the Software.
22065
503529c65456 Remove trailing whitespace from UPL license
Tom Rodriguez <tom.rodriguez@oracle.com>
parents: 22046
diff changeset
32 *
21997
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
c07e64ecb528 Update SL copyright headers to use UPL
Gilles Duboscq <gilles.m.duboscq@oracle.com>
parents: 21960
diff changeset
39 * SOFTWARE.
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
40 */
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
41 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
42
22259
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
43 import java.math.BigInteger;
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
44 import java.util.ArrayList;
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
45 import java.util.HashMap;
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
46 import java.util.List;
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
47 import java.util.Map;
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
48
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
49 import com.oracle.truffle.api.frame.FrameDescriptor;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
50 import com.oracle.truffle.api.frame.FrameSlot;
22259
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
51 import com.oracle.truffle.api.instrument.Instrumenter;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
52 import com.oracle.truffle.api.nodes.Node;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
53 import com.oracle.truffle.api.source.Source;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
54 import com.oracle.truffle.api.source.SourceSection;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
55 import com.oracle.truffle.sl.nodes.SLExpressionNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
56 import com.oracle.truffle.sl.nodes.SLRootNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
57 import com.oracle.truffle.sl.nodes.SLStatementNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
58 import com.oracle.truffle.sl.nodes.access.SLReadPropertyNode;
22415
c1f804ce6cad Extend interop functionality for SL
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22259
diff changeset
59 import com.oracle.truffle.sl.nodes.access.SLReadPropertyNodeGen;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
60 import com.oracle.truffle.sl.nodes.access.SLWritePropertyNode;
22415
c1f804ce6cad Extend interop functionality for SL
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22259
diff changeset
61 import com.oracle.truffle.sl.nodes.access.SLWritePropertyNodeGen;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
62 import com.oracle.truffle.sl.nodes.call.SLInvokeNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
63 import com.oracle.truffle.sl.nodes.call.SLInvokeNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
64 import com.oracle.truffle.sl.nodes.controlflow.SLBlockNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
65 import com.oracle.truffle.sl.nodes.controlflow.SLBreakNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
66 import com.oracle.truffle.sl.nodes.controlflow.SLContinueNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
67 import com.oracle.truffle.sl.nodes.controlflow.SLFunctionBodyNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
68 import com.oracle.truffle.sl.nodes.controlflow.SLIfNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
69 import com.oracle.truffle.sl.nodes.controlflow.SLReturnNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
70 import com.oracle.truffle.sl.nodes.controlflow.SLWhileNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
71 import com.oracle.truffle.sl.nodes.expression.SLAddNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
72 import com.oracle.truffle.sl.nodes.expression.SLBigIntegerLiteralNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
73 import com.oracle.truffle.sl.nodes.expression.SLDivNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
74 import com.oracle.truffle.sl.nodes.expression.SLEqualNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
75 import com.oracle.truffle.sl.nodes.expression.SLFunctionLiteralNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
76 import com.oracle.truffle.sl.nodes.expression.SLLessOrEqualNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
77 import com.oracle.truffle.sl.nodes.expression.SLLessThanNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
78 import com.oracle.truffle.sl.nodes.expression.SLLogicalAndNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
79 import com.oracle.truffle.sl.nodes.expression.SLLogicalNotNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
80 import com.oracle.truffle.sl.nodes.expression.SLLogicalOrNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
81 import com.oracle.truffle.sl.nodes.expression.SLLongLiteralNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
82 import com.oracle.truffle.sl.nodes.expression.SLMulNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
83 import com.oracle.truffle.sl.nodes.expression.SLParenExpressionNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
84 import com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
85 import com.oracle.truffle.sl.nodes.expression.SLSubNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
86 import com.oracle.truffle.sl.nodes.local.SLReadArgumentNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
87 import com.oracle.truffle.sl.nodes.local.SLReadLocalVariableNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
88 import com.oracle.truffle.sl.nodes.local.SLReadLocalVariableNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
89 import com.oracle.truffle.sl.nodes.local.SLWriteLocalVariableNode;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
90 import com.oracle.truffle.sl.nodes.local.SLWriteLocalVariableNodeGen;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22065
diff changeset
91 import com.oracle.truffle.sl.runtime.SLContext;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
92
13836
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
93 /**
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
94 * 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
95 * 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
96 */
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
97 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
98
13836
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
99 /**
64c77f0577bb More documentation and improvements of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13821
diff changeset
100 * 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
101 * 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
102 * 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
103 */
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
104 static class LexicalScope {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
105 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
106 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
107
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
108 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
109 this.outer = outer;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
110 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
111 if (outer != null) {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
112 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
113 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
114 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
115 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
116
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
117 /* 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
118 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
119 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
120
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
121 /* State while parsing a function. */
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
122 private int functionStartPos;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
123 private String functionName;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
124 private int functionBodyStartPos; // includes parameter list
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
125 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
126 private FrameDescriptor frameDescriptor;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
127 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
128
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
129 /* 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
130 private LexicalScope lexicalScope;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
131
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
132 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
133 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
134 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
135 }
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
136
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
137 public void startFunction(Token nameToken, int bodyStartPos) {
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
138 assert functionStartPos == 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
139 assert functionName == null;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
140 assert functionBodyStartPos == 0;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
141 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
142 assert frameDescriptor == null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
143 assert lexicalScope == null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
144
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
145 functionStartPos = nameToken.charPos;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
146 functionName = nameToken.val;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
147 functionBodyStartPos = bodyStartPos;
9258
07f8d136a05e Truffle API changes for the Frame API. Introduction of Assumptions class.
Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
parents: 9222
diff changeset
148 frameDescriptor = new FrameDescriptor();
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
149 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
150 startBlock();
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
151 }
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
152
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
153 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
154 /*
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
155 * 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
156 * 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
157 * specialized.
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
158 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
159 final SourceSection src = srcFromToken(nameToken);
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
160 final SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
161 methodNodes.add(createAssignment(nameToken, readArg));
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
162 parameterCount++;
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
163 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
164
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
165 public void finishFunction(SLStatementNode bodyNode) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
166 methodNodes.add(bodyNode);
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
167 final int bodyEndPos = bodyNode.getSourceSection().getCharEndIndex();
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
168 final SourceSection functionSrc = source.createSection(functionName, functionStartPos, bodyEndPos - functionStartPos);
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
169 final SLStatementNode methodBlock = finishBlock(methodNodes, functionBodyStartPos, bodyEndPos - functionBodyStartPos);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
170 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
171
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
172 final SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(functionSrc, methodBlock);
22251
8dddde8b20d4 Truffle/SL: provide an "unavailable" SourceSection for every RootNode, using a newly defined static name that identifies "SL Builtin" RootNodes.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157
diff changeset
173 final SLRootNode rootNode = new SLRootNode(this.context, frameDescriptor, functionBodyNode, functionSrc, functionName);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
174
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
175 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
176
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
177 functionStartPos = 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
178 functionName = null;
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
179 functionBodyStartPos = 0;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
180 parameterCount = 0;
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
181 frameDescriptor = null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
182 lexicalScope = null;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
183 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
184
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
185 public void startBlock() {
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
186 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
187 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
188
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
189 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes, int startPos, int length) {
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
190 lexicalScope = lexicalScope.outer;
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
191
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
192 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
193 flattenBlocks(bodyNodes, flattenedNodes);
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
194
16687
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
195 final SourceSection src = source.createSection("block", startPos, length);
7c8ddb4233cd SL/SourceAttribution: restore some attribution fixes that were lost in a tussle with hg; turn off tracing code in SL tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16685
diff changeset
196 return new SLBlockNode(src, 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
197 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
198
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
199 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
200 for (Node n : bodyNodes) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
201 if (n instanceof SLBlockNode) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
202 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
203 } else {
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
204 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
205 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
206 }
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
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
209 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
210 * Returns an {@link SLBreakNode} for the given token.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
211 *
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
212 * @param breakToken The token containing the break node's info.
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
213 * @return A SLBreakNode for the given token.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
214 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
215 public SLStatementNode createBreak(Token breakToken) {
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
216 final SLBreakNode breakNode = new SLBreakNode(srcFromToken(breakToken));
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
217 return breakNode;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
218 }
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
219
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
220 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
221 * Returns an {@link SLContinueNode} for the given token.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
222 *
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
223 * @param continueToken The token containing the continue node's info.
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
224 * @return A SLContinueNode built using the given token.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
225 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
226 public SLStatementNode createContinue(Token continueToken) {
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
227 final SLContinueNode continueNode = new SLContinueNode(srcFromToken(continueToken));
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
228 return continueNode;
7292
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
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
231 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
232 * Returns an {@link SLWhileNode} for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
233 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
234 * @param whileToken The token containing the while node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
235 * @param conditionNode The conditional node for this while loop
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
236 * @param bodyNode The body of the while loop
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
237 * @return A SLWhileNode built using the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
238 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
239 public SLStatementNode createWhile(Token whileToken, SLExpressionNode conditionNode, SLStatementNode bodyNode) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
240 final int start = whileToken.charPos;
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
241 final int end = bodyNode.getSourceSection().getCharEndIndex();
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
242 final SLWhileNode whileNode = new SLWhileNode(source.createSection(whileToken.val, start, end - start), conditionNode, bodyNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
243 return whileNode;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
244 }
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
245
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
246 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
247 * Returns an {@link SLIfNode} for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
248 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
249 * @param ifToken The token containing the if node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
250 * @param conditionNode The condition node of this if statement
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
251 * @param thenPartNode The then part of the if
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
252 * @param elsePartNode The else part of the if
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
253 * @return An SLIfNode for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
254 */
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
255 public SLStatementNode createIf(Token ifToken, SLExpressionNode conditionNode, SLStatementNode thenPartNode, SLStatementNode elsePartNode) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
256 final int start = ifToken.charPos;
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
257 final int end = elsePartNode == null ? thenPartNode.getSourceSection().getCharEndIndex() : elsePartNode.getSourceSection().getCharEndIndex();
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
258 final SLIfNode ifNode = new SLIfNode(source.createSection(ifToken.val, start, end - start), conditionNode, thenPartNode, elsePartNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
259 return ifNode;
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
260 }
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
261
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
262 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
263 * Returns an {@link SLReturnNode} for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
264 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
265 * @param t The token containing the return node's info
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
266 * @param valueNode The value of the return
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
267 * @return An SLReturnNode for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
268 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
269 public SLStatementNode createReturn(Token t, SLExpressionNode valueNode) {
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
270 final int start = t.charPos;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
271 final int length = valueNode == null ? t.val.length() : valueNode.getSourceSection().getCharEndIndex() - start;
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
272 final SLReturnNode returnNode = new SLReturnNode(source.createSection(t.val, start, length), valueNode);
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
273 return returnNode;
7292
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
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
276 /**
22003
5bc7f7b867ab Making debugger always on for each TruffleVM execution. Introducing EventConsumer to process such debugger events. Requesting each RootNode to be associated with a TruffleLanguage, so debugger can find out proper context for each Node where executions gets suspended.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21997
diff changeset
277 * Returns the corresponding subclass of {@link SLExpressionNode} for binary expressions. </br>
5bc7f7b867ab Making debugger always on for each TruffleVM execution. Introducing EventConsumer to process such debugger events. Requesting each RootNode to be associated with a TruffleLanguage, so debugger can find out proper context for each Node where executions gets suspended.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21997
diff changeset
278 * These nodes are currently not instrumented.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
279 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
280 * @param opToken The operator of the binary expression
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
281 * @param leftNode The left node of the expression
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
282 * @param rightNode The right node of the expression
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
283 * @return A subclass of SLExpressionNode using the given parameters based on the given opToken.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
284 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
285 public SLExpressionNode createBinary(Token opToken, SLExpressionNode leftNode, SLExpressionNode rightNode) {
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
286 int start = leftNode.getSourceSection().getCharIndex();
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
287 int length = rightNode.getSourceSection().getCharEndIndex() - start;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
288 final SourceSection src = source.createSection(opToken.val, start, length);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
289 switch (opToken.val) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
290 case "+":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
291 return SLAddNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
292 case "*":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
293 return SLMulNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
294 case "/":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
295 return SLDivNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
296 case "-":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
297 return SLSubNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
298 case "<":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
299 return SLLessThanNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
300 case "<=":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
301 return SLLessOrEqualNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
302 case ">":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
303 return SLLogicalNotNodeGen.create(src, SLLessOrEqualNodeGen.create(null, leftNode, rightNode));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
304 case ">=":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
305 return SLLogicalNotNodeGen.create(src, SLLessThanNodeGen.create(null, leftNode, rightNode));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
306 case "==":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
307 return SLEqualNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
308 case "!=":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
309 return SLLogicalNotNodeGen.create(src, SLEqualNodeGen.create(null, leftNode, rightNode));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
310 case "&&":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
311 return SLLogicalAndNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
312 case "||":
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
313 return SLLogicalOrNodeGen.create(src, leftNode, rightNode);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
314 default:
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
315 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
316 }
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
317 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
318
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
319 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
320 * Returns an {@link SLInvokeNode} for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
321 *
18411
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
322 * @param functionNode The function being called
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
323 * @param parameterNodes The parameters of the function call
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
324 * @param finalToken A token used to determine the end of the sourceSelection for this call
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
325 * @return An SLInvokeNode for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
326 */
18411
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
327 public SLExpressionNode createCall(SLExpressionNode functionNode, List<SLExpressionNode> parameterNodes, Token finalToken) {
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
328 final int startPos = functionNode.getSourceSection().getCharIndex();
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
329 final int endPos = finalToken.charPos + finalToken.val.length();
18411
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
330 final SourceSection src = source.createSection(functionNode.getSourceSection().getIdentifier(), startPos, endPos - startPos);
21960
a88981c5ce8b Initial test for Java Interop: Perform callback to Math.min and Math.max via TruffleObject and Message.createExecute(2).
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
331 return SLInvokeNodeGen.create(src, parameterNodes.toArray(new SLExpressionNode[parameterNodes.size()]), functionNode);
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
332 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
333
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
334 /**
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
335 * Returns an {@link SLWriteLocalVariableNode} for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
336 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
337 * @param nameToken The name of the variable being assigned
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
338 * @param valueNode The value to be assigned
16880
7661cc464239 Truffle/Instrumentation: Added Instrumentable interface and LineLocationToSourceSections map
David Piorkowski <david.piorkowski@oracle.com>
parents: 16702
diff changeset
339 * @return An SLExpressionNode for the given parameters.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
340 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
341 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
342 FrameSlot frameSlot = frameDescriptor.findOrAddFrameSlot(nameToken.val);
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
343 lexicalScope.locals.put(nameToken.val, frameSlot);
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
344 final int start = nameToken.charPos;
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
345 final int length = valueNode.getSourceSection().getCharEndIndex() - start;
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
346 return SLWriteLocalVariableNodeGen.create(source.createSection("=", start, length), 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
347 }
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
348
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
349 /**
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
350 * Returns a {@link SLReadLocalVariableNode} if this read is a local variable or a
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
351 * {@link SLFunctionLiteralNode} if this read is global. In Simple, the only global names are
22259
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
352 * functions. </br> There is currently no instrumentation{@linkplain Instrumenter
1348cc2e084e Truffle/Instrumentation: javadoc
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22251
diff changeset
353 * Instrumentation} for this node.
16702
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
354 *
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
355 * @param nameToken The name of the variable/function being read
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
356 * @return either:
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
357 * <ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
358 * <li>A SLReadLocalVariableNode representing the local variable being read.</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
359 * <li>A SLFunctionLiteralNode representing the function definition</li>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
360 * </ul>
2a5ec181dad4 SL: Added instrumentation testing
David Piorkowski <david.piorkowski@oracle.com>
parents: 16687
diff changeset
361 */
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
362 public SLExpressionNode createRead(Token nameToken) {
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
363 final FrameSlot frameSlot = lexicalScope.locals.get(nameToken.val);
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
364 final SourceSection src = srcFromToken(nameToken);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
365 if (frameSlot != null) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
366 /* Read of a local variable. */
18763
301fea50e42e SL: migrate SL to use @GenerateNodeFactory.
Christian Humer <christian.humer@gmail.com>
parents: 18411
diff changeset
367 return SLReadLocalVariableNodeGen.create(src, frameSlot);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
368 } else {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
369 /* Read of a global name. In our language, the only global names are functions. */
22046
e7c2d36daf72 TruffleLanguage.parse method to convert a source to CallTarget. Basic caching to make sure the code is shared among tenants in one JVM.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22003
diff changeset
370 return new SLFunctionLiteralNode(src, nameToken.val);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
371 }
13761
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
372 }
7c418666c6c9 Refactoring and cleanup of Simple Language (more to come soon)
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13276
diff changeset
373
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
374 public SLExpressionNode createStringLiteral(Token literalToken) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
375 /* Remove the trailing and ending " */
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
376 String literal = literalToken.val;
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
377 assert literal.length() >= 2 && literal.startsWith("\"") && literal.endsWith("\"");
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
378 final SourceSection src = srcFromToken(literalToken);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
379 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
380
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
381 return new SLStringLiteralNode(src, literal);
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
382 }
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
383
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
384 public SLExpressionNode createNumericLiteral(Token literalToken) {
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
385 final SourceSection src = srcFromToken(literalToken);
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
386 try {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
387 /* Try if the literal is small enough to fit into a long value. */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
388 return new SLLongLiteralNode(src, Long.parseLong(literalToken.val));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
389 } catch (NumberFormatException ex) {
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
390 /* Overflow of long value, so fall back to BigInteger. */
16512
abe7128ca473 SL: upgrade source attribution
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16067
diff changeset
391 return new SLBigIntegerLiteralNode(src, new BigInteger(literalToken.val));
13821
b16ec83edc73 Documentation and more refactoring of Simple Language
Christian Wimmer <christian.wimmer@oracle.com>
parents: 13805
diff changeset
392 }
7292
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
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
395 public SLExpressionNode createParenExpression(SLExpressionNode expressionNode, int start, int length) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
396 final SourceSection src = source.createSection("()", start, length);
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
397 return new SLParenExpressionNode(src, expressionNode);
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
398 }
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
399
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
400 /**
18411
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
401 * Returns an {@link SLReadPropertyNode} for the given parameters.
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
402 *
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
403 * @param receiverNode The receiver of the property access
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
404 * @param nameToken The name of the property being accessed
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
405 * @return An SLExpressionNode for the given parameters.
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
406 */
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
407 public SLExpressionNode createReadProperty(SLExpressionNode receiverNode, Token nameToken) {
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
408 final int startPos = receiverNode.getSourceSection().getCharIndex();
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
409 final int endPos = nameToken.charPos + nameToken.val.length();
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
410 final SourceSection src = source.createSection(".", startPos, endPos - startPos);
22415
c1f804ce6cad Extend interop functionality for SL
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22259
diff changeset
411 return SLReadPropertyNodeGen.create(src, nameToken.val, receiverNode);
18411
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
412 }
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
413
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
414 /**
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
415 * Returns an {@link SLWritePropertyNode} for the given parameters.
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
416 *
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
417 * @param receiverNode The receiver object of the property assignment
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
418 * @param nameToken The name of the property being assigned
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
419 * @param valueNode The value to be assigned
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
420 * @return An SLExpressionNode for the given parameters.
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
421 */
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
422 public SLExpressionNode createWriteProperty(SLExpressionNode receiverNode, Token nameToken, SLExpressionNode valueNode) {
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
423 final int start = receiverNode.getSourceSection().getCharIndex();
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
424 final int length = valueNode.getSourceSection().getCharEndIndex() - start;
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
425 SourceSection src = source.createSection("=", start, length);
22415
c1f804ce6cad Extend interop functionality for SL
Matthias Grimmer <grimmer@ssw.jku.at>
parents: 22259
diff changeset
426 return SLWritePropertyNodeGen.create(src, nameToken.val, receiverNode, valueNode);
18411
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
427 }
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
428
dc2e000bed40 SL: add basic support for creating objects and accessing properties
Andreas Woess <andreas.woess@jku.at>
parents: 17443
diff changeset
429 /**
16675
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
430 * Creates source description of a single token.
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
431 */
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
432 private SourceSection srcFromToken(Token token) {
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
433 return source.createSection(token.val, token.charPos, token.val.length());
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
434 }
0fc43b066eee SL/SourceAttribution: correct some omissions, and in particular add new node SLParenExpressionNode to represent a parenthesized expression; this is semantically neutral of course, but needed to account correctly for the text of such an expression (as opposed to its contents).
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16595
diff changeset
435
7292
213c1297a814 Simple Language: A simple dynamic programming language to demonstrate Truffle features
Christian Wimmer <christian.wimmer@oracle.com>
parents:
diff changeset
436 }