comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java @ 16685:888907296590

Backed out changeset: d654cd5ed05a
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Fri, 01 Aug 2014 18:14:43 -0700
parents d654cd5ed05a
children 7c8ddb4233cd
comparison
equal deleted inserted replaced
16684:d654cd5ed05a 16685:888907296590
63 /* State while parsing a source unit. */ 63 /* State while parsing a source unit. */
64 private final SLContext context; 64 private final SLContext context;
65 private final Source source; 65 private final Source source;
66 66
67 /* State while parsing a function. */ 67 /* State while parsing a function. */
68 private int functionStartPos;
69 private String functionName; 68 private String functionName;
70 private int functionBodyStartPos; // includes parameter list
71 private int parameterCount; 69 private int parameterCount;
72 private FrameDescriptor frameDescriptor; 70 private FrameDescriptor frameDescriptor;
73 private List<SLStatementNode> methodNodes; 71 private List<SLStatementNode> methodNodes;
74 72
75 /* State while parsing a block. */ 73 /* State while parsing a block. */
81 this.context = context; 79 this.context = context;
82 this.source = source; 80 this.source = source;
83 this.prober = prober; 81 this.prober = prober;
84 } 82 }
85 83
86 public void startFunction(Token nameToken, int bodyStartPos) { 84 public void startFunction(Token nameToken) {
87 assert functionStartPos == 0;
88 assert functionName == null; 85 assert functionName == null;
89 assert functionBodyStartPos == 0;
90 assert parameterCount == 0; 86 assert parameterCount == 0;
91 assert frameDescriptor == null; 87 assert frameDescriptor == null;
92 assert lexicalScope == null; 88 assert lexicalScope == null;
93 89
94 functionStartPos = nameToken.charPos;
95 functionName = nameToken.val; 90 functionName = nameToken.val;
96 functionBodyStartPos = bodyStartPos;
97 frameDescriptor = new FrameDescriptor(); 91 frameDescriptor = new FrameDescriptor();
98 methodNodes = new ArrayList<>(); 92 methodNodes = new ArrayList<>();
99 startBlock(); 93 startBlock();
100 } 94 }
101 95
104 * Method parameters are assigned to local variables at the beginning of the method. This 98 * Method parameters are assigned to local variables at the beginning of the method. This
105 * ensures that accesses to parameters are specialized the same way as local variables are 99 * ensures that accesses to parameters are specialized the same way as local variables are
106 * specialized. 100 * specialized.
107 */ 101 */
108 final SourceSection src = srcFromToken(nameToken); 102 final SourceSection src = srcFromToken(nameToken);
109 final SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount); 103 SLReadArgumentNode readArg = new SLReadArgumentNode(src, parameterCount);
110 methodNodes.add(createAssignment(nameToken, readArg)); 104 methodNodes.add(createAssignment(nameToken, readArg));
111 parameterCount++; 105 parameterCount++;
112 } 106 }
113 107
114 public void finishFunction(SLStatementNode bodyNode) { 108 public void finishFunction(SLStatementNode bodyNode) {
115 methodNodes.add(bodyNode); 109 methodNodes.add(bodyNode);
116 final int bodyEndPos = bodyNode.getSourceSection().getCharEndIndex(); 110 // TODO (mlvdv) testing
117 final SourceSection functionSrc = source.createSection(functionName, functionStartPos, bodyEndPos - functionStartPos); 111 SLStatementNode methodBlock = finishBlock(methodNodes, -1, -1);
118 final SLStatementNode methodBlock = finishBlock(methodNodes, functionBodyStartPos, bodyEndPos - functionBodyStartPos);
119 assert lexicalScope == null : "Wrong scoping of blocks in parser"; 112 assert lexicalScope == null : "Wrong scoping of blocks in parser";
120 113
121 SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(functionSrc, methodBlock); 114 SLFunctionBodyNode functionBodyNode = new SLFunctionBodyNode(methodBlock);
122 SLRootNode rootNode = new SLRootNode(frameDescriptor, functionBodyNode, functionName); 115 SLRootNode rootNode = new SLRootNode(frameDescriptor, functionBodyNode, functionName);
123 116
124 context.getFunctionRegistry().register(functionName, rootNode); 117 context.getFunctionRegistry().register(functionName, rootNode);
125 118
126 functionStartPos = 0;
127 functionName = null; 119 functionName = null;
128 functionBodyStartPos = 0;
129 parameterCount = 0; 120 parameterCount = 0;
130 frameDescriptor = null; 121 frameDescriptor = null;
131 lexicalScope = null; 122 lexicalScope = null;
132 } 123 }
133 124
134 public void startBlock() { 125 public void startBlock() {
135 lexicalScope = new LexicalScope(lexicalScope); 126 lexicalScope = new LexicalScope(lexicalScope);
136 } 127 }
137 128
138 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes, int startPos, int length) { 129 public SLStatementNode finishBlock(List<SLStatementNode> bodyNodes, int lBracePos, int length) {
139 lexicalScope = lexicalScope.outer; 130 lexicalScope = lexicalScope.outer;
140 131
141 List<SLStatementNode> flattenedNodes = new ArrayList<>(bodyNodes.size()); 132 List<SLStatementNode> flattenedNodes = new ArrayList<>(bodyNodes.size());
142 flattenBlocks(bodyNodes, flattenedNodes); 133 flattenBlocks(bodyNodes, flattenedNodes);
143 134
144 if (startPos >= 0) { 135 if (lBracePos >= 0) {
145 final SourceSection src = source.createSection("block", startPos, length); 136 final SourceSection src = source.createSection("block", lBracePos, length);
146 return new SLBlockNode(src, flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()])); 137 return new SLBlockNode(src, flattenedNodes.toArray(new SLStatementNode[flattenedNodes.size()]));
147 } 138 }
148 if (flattenedNodes.size() == 0) { 139 if (flattenedNodes.size() == 0) {
149 // TODO (mlvdv) for error reporting, should have the character position, even if the 140 // TODO (mlvdv) for error reporting, should have the character position, even if the
150 // block is empty. 141 // block is empty.