Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/NodeFactory.java @ 7292:213c1297a814
Simple Language: A simple dynamic programming language to demonstrate Truffle features
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Fri, 21 Dec 2012 10:45:37 -0800 |
parents | |
children | 31da1716950f |
comparison
equal
deleted
inserted
replaced
7291:a748e4d44694 | 7292:213c1297a814 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.sl; | |
24 | |
25 import java.io.*; | |
26 import java.math.*; | |
27 import java.util.*; | |
28 | |
29 import com.oracle.truffle.api.frame.*; | |
30 import com.oracle.truffle.sl.nodes.*; | |
31 import com.oracle.truffle.sl.ops.*; | |
32 import com.oracle.truffle.sl.types.*; | |
33 | |
34 public class NodeFactory { | |
35 | |
36 private final HashMap<String, FunctionDefinitionNode> functions; | |
37 private final PrintStream printOutput; | |
38 | |
39 private FrameDescriptor frameDescriptor; | |
40 private TypedNode returnValue; | |
41 | |
42 public NodeFactory(PrintStream printOutput) { | |
43 this.functions = new HashMap<>(); | |
44 this.printOutput = printOutput; | |
45 } | |
46 | |
47 public FunctionDefinitionNode findFunction(String name) { | |
48 return functions.get(name); | |
49 } | |
50 | |
51 public void startFunction() { | |
52 frameDescriptor = new FrameDescriptor(TypesGen.TYPES); | |
53 } | |
54 | |
55 public void createFunction(StatementNode body, String name) { | |
56 functions.put(name, new FunctionDefinitionNode(body, frameDescriptor, name, returnValue)); | |
57 } | |
58 | |
59 public TypedNode createLocal(String name) { | |
60 return ReadLocalOpFactory.create(frameDescriptor.findOrAddFrameSlot(name)); | |
61 } | |
62 | |
63 public TypedNode createStringLiteral(String value) { | |
64 return StringLiteralFactory.create(value); | |
65 } | |
66 | |
67 public StatementNode createAssignment(String name, TypedNode right) { | |
68 return WriteLocalOpFactory.create(right, frameDescriptor.findOrAddFrameSlot(name)); | |
69 } | |
70 | |
71 public StatementNode createPrint(List<TypedNode> expressions) { | |
72 return new PrintNode(expressions, printOutput); | |
73 } | |
74 | |
75 public StatementNode createWhile(ConditionNode condition, StatementNode body) { | |
76 return new WhileNode(condition, body); | |
77 } | |
78 | |
79 public StatementNode createBlock(List<StatementNode> statements) { | |
80 return new BlockNode(statements.toArray(new StatementNode[statements.size()])); | |
81 } | |
82 | |
83 public TypedNode createBinary(String operation, TypedNode left, TypedNode right) { | |
84 switch (operation) { | |
85 case "+": | |
86 return AddOpFactory.create(left, right); | |
87 case "*": | |
88 return MulOpFactory.create(left, right); | |
89 case "<": | |
90 return LessThanOpFactory.create(left, right); | |
91 case "&&": | |
92 return LogicalAndOpFactory.create(left, right); | |
93 default: | |
94 throw new RuntimeException("unexpected operation: " + operation); | |
95 } | |
96 } | |
97 | |
98 public TypedNode createNumericLiteral(String value) { | |
99 try { | |
100 return IntegerLiteralFactory.create(Integer.parseInt(value)); | |
101 } catch (NumberFormatException ex) { | |
102 return BigIntegerLiteralFactory.create(new BigInteger(value)); | |
103 } | |
104 } | |
105 | |
106 public TypedNode createTime() { | |
107 return TimeOpFactory.create(); | |
108 } | |
109 | |
110 public StatementNode createReturn(TypedNode value) { | |
111 FrameSlot slot = frameDescriptor.findOrAddFrameSlot("<retval>"); | |
112 if (returnValue == null) { | |
113 returnValue = ReadLocalOpFactory.create(slot); | |
114 } | |
115 StatementNode write = WriteLocalOpFactory.create(value, slot); | |
116 return new ReturnNode(write); | |
117 } | |
118 } |