├── golang ├── .ast ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── oracle │ │ │ └── app │ │ │ ├── parser │ │ │ └── ir │ │ │ │ ├── GoIRVisitable.java │ │ │ │ ├── nodes │ │ │ │ ├── GoIRTypes.java │ │ │ │ ├── GoIRDeclStmtNode.java │ │ │ │ ├── GoIRExprNode.java │ │ │ │ ├── GoIRFieldListNode.java │ │ │ │ ├── GoIRArrayFieldNode.java │ │ │ │ ├── GoIRMapTypeNode.java │ │ │ │ ├── GoIRStmtNode.java │ │ │ │ ├── GoIRStructTypeNode.java │ │ │ │ ├── GoIRSelectorExprNode.java │ │ │ │ ├── GoIRImportSpecNode.java │ │ │ │ ├── GoIRIncDecStmtNode.java │ │ │ │ ├── GoIRObjectNode.java │ │ │ │ ├── GoIRBranchStmtNode.java │ │ │ │ ├── GoIRFileNode.java │ │ │ │ ├── GoIRExprStmtNode.java │ │ │ │ ├── GoIRKeyValueNode.java │ │ │ │ ├── GoIRValueSpecNode.java │ │ │ │ ├── GoIRFieldNode.java │ │ │ │ ├── GoIRStarNode.java │ │ │ │ ├── GoIRTypeSpecNode.java │ │ │ │ ├── GoIRBlockStmtNode.java │ │ │ │ ├── GoIRArrayTypeNode.java │ │ │ │ ├── GoIRForNode.java │ │ │ │ ├── GoIRUnaryNode.java │ │ │ │ ├── GoIRFuncTypeNode.java │ │ │ │ ├── GoIRReturnStmtNode.java │ │ │ │ ├── GoIRGenDeclNode.java │ │ │ │ ├── GoIRSwitchStmtNode.java │ │ │ │ ├── GoIRAssignmentStmtNode.java │ │ │ │ ├── GoIRBinaryExprNode.java │ │ │ │ ├── GoIRIfStmtNode.java │ │ │ │ ├── GoIRCaseClauseNode.java │ │ │ │ ├── GoTempIRNode.java │ │ │ │ ├── GoIRCompositeLitNode.java │ │ │ │ ├── GoIRFuncDeclNode.java │ │ │ │ ├── GoIRFloat32Node.java │ │ │ │ ├── GoIRFloat64Node.java │ │ │ │ ├── GoIRIntNode.java │ │ │ │ ├── GoIRStringNode.java │ │ │ │ ├── GoIRIndexNode.java │ │ │ │ ├── GoIRArrayListExprNode.java │ │ │ │ ├── GoIRIdentNode.java │ │ │ │ ├── GoIRInvokeNode.java │ │ │ │ ├── GoIRSliceExprNode.java │ │ │ │ └── GoIRBasicLitNode.java │ │ │ │ └── GoBaseIRNode.java │ │ │ ├── nodes │ │ │ ├── types │ │ │ │ ├── GoPrimitiveTypes.java │ │ │ │ ├── GoPrimitiveType.java │ │ │ │ ├── GoArrayLikeTypes.java │ │ │ │ ├── GoNonPrimitiveType.java │ │ │ │ ├── GoStringNode.java │ │ │ │ ├── GoFloat64Node.java │ │ │ │ ├── GoIntNode.java │ │ │ │ ├── GoFloat32Node.java │ │ │ │ ├── FieldNode.java │ │ │ │ ├── GoStruct.java │ │ │ │ ├── GoBooleanNode.java │ │ │ │ └── GoFunctionLiteralNode.java │ │ │ ├── GoIncDecStmtNode.java │ │ │ ├── GoUnaryNode.java │ │ │ ├── controlflow │ │ │ │ ├── GoBreakException.java │ │ │ │ ├── GoContinueException.java │ │ │ │ ├── GoBreakNode.java │ │ │ │ ├── GoContinueNode.java │ │ │ │ ├── GoReturnException.java │ │ │ │ ├── GoSwitchNode.java │ │ │ │ ├── GoForNode.java │ │ │ │ ├── GoReturnNode.java │ │ │ │ ├── GoFunctionBodyNode.java │ │ │ │ ├── GoForBreakNode.java │ │ │ │ ├── GoIfStmtNode.java │ │ │ │ ├── GoForWhileNode.java │ │ │ │ ├── GoCaseClauseNode.java │ │ │ │ └── GoForRepeatingNode.java │ │ │ ├── expression │ │ │ │ ├── GoLogicalNotNode.java │ │ │ │ ├── GoBitwiseOrNode.java │ │ │ │ ├── GoBitwiseAndNode.java │ │ │ │ ├── GoBinaryLeftShiftNode.java │ │ │ │ ├── GoBitwiseXORNode.java │ │ │ │ ├── GoBitwiseComplementNode.java │ │ │ │ ├── GoBinaryRightShiftNode.java │ │ │ │ ├── GoDecNode.java │ │ │ │ ├── GoIncNode.java │ │ │ │ ├── GoLogicalOrNode.java │ │ │ │ ├── GoNegativeSignNode.java │ │ │ │ ├── GoPositiveSignNode.java │ │ │ │ ├── GoModNode.java │ │ │ │ ├── GoLessThanNode.java │ │ │ │ ├── GoLessOrEqualNode.java │ │ │ │ ├── GoGreaterOrEqualNode.java │ │ │ │ ├── GoMulNode.java │ │ │ │ ├── GoMapTypeExprNode.java │ │ │ │ ├── GoGreaterThanNode.java │ │ │ │ ├── GoSubNode.java │ │ │ │ ├── GoLogicalAndNode.java │ │ │ │ ├── GoKeyValueNode.java │ │ │ │ ├── GoStarExpressionNode.java │ │ │ │ ├── GoParenExpressionNode.java │ │ │ │ ├── GoAddNode.java │ │ │ │ ├── GoUnaryAddressNode.java │ │ │ │ ├── GoDivNode.java │ │ │ │ ├── GoStructTypeExprNode.java │ │ │ │ ├── GoNotEqualNode.java │ │ │ │ ├── GoSliceExprNode.java │ │ │ │ ├── GoShortCircuitNode.java │ │ │ │ └── GoEqualNode.java │ │ │ ├── GoBinaryNode.java │ │ │ ├── GoUndefinedFunctionRootNode.java │ │ │ ├── interop │ │ │ │ ├── GoTypeToForeignNode.java │ │ │ │ └── GoForeignToGoTypeNode.java │ │ │ ├── GoExprNode.java │ │ │ ├── local │ │ │ │ ├── GoReadArgumentsNode.java │ │ │ │ ├── GoReadPropertyNode.java │ │ │ │ ├── GoStructPropertyWrite.java │ │ │ │ ├── GoWriteMemoryNode.java │ │ │ │ └── GoWriteProperty.java │ │ │ ├── call │ │ │ │ ├── GoFieldNode.java │ │ │ │ ├── GoFieldListNode.java │ │ │ │ ├── GoFuncTypeNode.java │ │ │ │ └── GoInvokeNode.java │ │ │ ├── GoIdentNode.java │ │ │ ├── GoArrayExprNode.java │ │ │ ├── GoTypes.java │ │ │ ├── SpecDecl │ │ │ │ ├── GoImportSpec.java │ │ │ │ └── GoSelectorExprNode.java │ │ │ └── GoEvalRootNode.java │ │ │ ├── builtins │ │ │ ├── GoLenBuiltin.java │ │ │ ├── GoCapBuiltin.java │ │ │ ├── GoDeleteBuiltin.java │ │ │ ├── GoBuiltinNode.java │ │ │ ├── fmt │ │ │ │ ├── GoFmtPrintln.java │ │ │ │ └── FmtFunctionList.java │ │ │ ├── GoMakeBuiltin.java │ │ │ ├── GoPrintlnBuiltin.java │ │ │ └── GoAppendBuiltin.java │ │ │ ├── runtime │ │ │ ├── GoNull.java │ │ │ ├── GoUndefinedNameException.java │ │ │ ├── GoNullMessageResolution.java │ │ │ ├── GoFunction.java │ │ │ ├── GoFunctionMessageResolution.java │ │ │ └── GoFunctionRegistry.java │ │ │ ├── GoMain.java │ │ │ └── ToolChain.java │ └── test │ │ └── java │ │ └── com │ │ └── oracle │ │ └── app │ │ ├── SimpleMath.go │ │ ├── global_variable.go │ │ ├── lexical_scope_same_ident.go │ │ ├── Array.go │ │ ├── declarations.go │ │ ├── forward_decl.go │ │ ├── PointerTest.java │ │ ├── SimpleMathTest.java │ │ ├── DeclarationsTest.java │ │ ├── GlobalVariableTest.java │ │ ├── ForwardFunctionDeclarationTest.java │ │ ├── LexicalScopeTest.java │ │ ├── FunctionParametersTest.java │ │ ├── ArrayTest.java │ │ ├── MultipleReturnsTest.java │ │ ├── StructTest.java │ │ ├── RecursionTest.java │ │ ├── ControlFlowTest.java │ │ └── SliceTest.java ├── parameters.go ├── simplemath.go ├── Assignment.go ├── printast.go ├── Test.go └── gt ├── sprints ├── Spring2018 │ ├── README │ ├── Plans │ │ ├── Sprint 1 Plan Release 2.pdf │ │ ├── Sprint 2 Plan Release 2.pdf │ │ ├── Sprint 3 Plan Release 2.pdf │ │ ├── Sprint 4 Plan Release 2.pdf │ │ ├── Sprint 5 Plan Release 2.pdf │ │ └── Sprint 6 Plan Release 2.pdf │ └── Reports │ │ ├── Sprint 1 Report Release 2.pdf │ │ ├── Sprint 2 Report Release 2.pdf │ │ ├── Sprint 3 Report Release 2.pdf │ │ ├── Sprint 4 Release 2 Report.pdf │ │ ├── Sprint 5 Release 2 Report.pdf │ │ └── Sprint 6 Release 2 Report.pdf ├── Winter2018 │ ├── README │ ├── Sprint 1 Plan.pdf │ ├── Sprint 2 Plan.pdf │ ├── Sprint 1 Report.pdf │ └── Sprint 2 Report.pdf ├── Release 2 Plan.pdf └── Release Plan.pdf ├── .gitignore ├── README.md └── docs └── css └── style.css /golang/.ast: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /sprints/Spring2018/README: -------------------------------------------------------------------------------- 1 | Sprint reports for spring quarter. 2 | -------------------------------------------------------------------------------- /sprints/Winter2018/README: -------------------------------------------------------------------------------- 1 | Sprint reports for winter quarter. 2 | -------------------------------------------------------------------------------- /sprints/Release 2 Plan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Release 2 Plan.pdf -------------------------------------------------------------------------------- /sprints/Release Plan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Release Plan.pdf -------------------------------------------------------------------------------- /sprints/Winter2018/Sprint 1 Plan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Winter2018/Sprint 1 Plan.pdf -------------------------------------------------------------------------------- /sprints/Winter2018/Sprint 2 Plan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Winter2018/Sprint 2 Plan.pdf -------------------------------------------------------------------------------- /sprints/Winter2018/Sprint 1 Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Winter2018/Sprint 1 Report.pdf -------------------------------------------------------------------------------- /sprints/Winter2018/Sprint 2 Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Winter2018/Sprint 2 Report.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Plans/Sprint 1 Plan Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Plans/Sprint 1 Plan Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Plans/Sprint 2 Plan Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Plans/Sprint 2 Plan Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Plans/Sprint 3 Plan Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Plans/Sprint 3 Plan Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Plans/Sprint 4 Plan Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Plans/Sprint 4 Plan Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Plans/Sprint 5 Plan Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Plans/Sprint 5 Plan Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Plans/Sprint 6 Plan Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Plans/Sprint 6 Plan Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Reports/Sprint 1 Report Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Reports/Sprint 1 Report Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Reports/Sprint 2 Report Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Reports/Sprint 2 Report Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Reports/Sprint 3 Report Release 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Reports/Sprint 3 Report Release 2.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Reports/Sprint 4 Release 2 Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Reports/Sprint 4 Release 2 Report.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Reports/Sprint 5 Release 2 Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Reports/Sprint 5 Release 2 Report.pdf -------------------------------------------------------------------------------- /sprints/Spring2018/Reports/Sprint 6 Release 2 Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PDZaninov/GoLang-Compiler/HEAD/sprints/Spring2018/Reports/Sprint 6 Release 2 Report.pdf -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/GoIRVisitable.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir; 2 | 3 | public interface GoIRVisitable { 4 | public Object accept(GoIRVisitor visitor); 5 | } 6 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/SimpleMath.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | 4 | func main() int{ 5 | var x,n int = 1,5 6 | for b := 1; b <= 5; b++{ 7 | x *=b 8 | } 9 | 10 | return x 11 | } -------------------------------------------------------------------------------- /golang/parameters.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func me() int { 4 | return add(10,11) 5 | } 6 | func add(x int, y int) int { 7 | return x + y 8 | } 9 | 10 | func main() { 11 | println(me()) 12 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/global_variable.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | var global int = 2 4 | 5 | func set() { 6 | global = 7 7 | } 8 | 9 | func main() int{ 10 | set() 11 | return global 12 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/lexical_scope_same_ident.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func scoping(x int) int { 4 | x = 3 5 | return x 6 | } 7 | 8 | func main() int{ 9 | x := 2 10 | y := scoping(x) 11 | return y 12 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoPrimitiveTypes.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | public enum GoPrimitiveTypes { 4 | INT, 5 | FLOAT32, 6 | FLOAT64, 7 | STRING, 8 | BOOL, 9 | OBJECT 10 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | graalvm* 2 | target/ 3 | .classpath 4 | .factorypath 5 | .project 6 | .idea/ 7 | .settings/ 8 | ./.project 9 | /bin/ 10 | bin/ 11 | .recommenders/ 12 | /.metadata/ 13 | *.iml 14 | printast 15 | UnitTest.ast 16 | UnitTest.go 17 | *.output 18 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoIncDecStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.truffle.api.dsl.NodeChild; 4 | 5 | @NodeChild("valueNode") 6 | public abstract class GoIncDecStmtNode extends GoExpressionNode { 7 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoUnaryNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.truffle.api.dsl.NodeChild; 4 | 5 | 6 | @NodeChild("valueNode") 7 | public abstract class GoUnaryNode extends GoExpressionNode{ 8 | 9 | } 10 | -------------------------------------------------------------------------------- /golang/simplemath.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func vals() (int,int){ 4 | var b int = 1 5 | var c int = 5 6 | 7 | return b,c 8 | 9 | } 10 | 11 | func main() { 12 | var x,y = vals() 13 | for b := 1; b <= 5; b++{ 14 | x *=b 15 | } 16 | println(x + y) 17 | var z = x + y 18 | 19 | println(z) 20 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoPrimitiveType.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | 5 | public abstract class GoPrimitiveType extends GoExpressionNode { 6 | 7 | public abstract GoPrimitiveTypes getType(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/Array.go: -------------------------------------------------------------------------------- 1 | package main 2 | func main() int{ 3 | var a [5] int 4 | result := 0 5 | for i := 0; i < len(a); i++{ 6 | result += a[i] 7 | } 8 | b := [5]int{1 , 2, 3, 4, 5} 9 | b[4] = 10 10 | for j := 0; j < len(b); j++ { 11 | result += b[j] 12 | } 13 | return result 14 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/declarations.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func main() int{ 4 | var k int = 1 5 | var x,y = 8,9 6 | z := 7 7 | var a,b = 1,2 8 | var c = 3 9 | d,f := 1,2 10 | g := 3 11 | y = 12 12 | array := make([]int,5) 13 | array[0] = 2 14 | sum := a + b + c + d + f + g + k + x + y + z + array[0] 15 | return sum 16 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/GoLenBuiltin.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins; 2 | 3 | import com.oracle.app.nodes.types.GoArrayLikeTypes; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "len") 8 | public abstract class GoLenBuiltin extends GoBuiltinNode{ 9 | 10 | @Specialization 11 | public int len(GoArrayLikeTypes a) { 12 | return a.len(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoBreakException.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.truffle.api.nodes.ControlFlowException; 4 | 5 | public final class GoBreakException extends ControlFlowException { 6 | 7 | public static final GoBreakException SINGLETON = new GoBreakException(); 8 | 9 | private static final long serialVersionUID = -91013036379258890L; 10 | 11 | private GoBreakException() {} 12 | } 13 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/GoCapBuiltin.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins; 2 | 3 | import com.oracle.app.nodes.types.GoArrayLikeTypes; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "cap") 8 | public abstract class GoCapBuiltin extends GoBuiltinNode{ 9 | 10 | @Specialization 11 | public int cap(GoArrayLikeTypes array){ 12 | return array.cap(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TruffleGoLang-Compiler 2 | Contributors: Baiwen Huang, Kyle Remmert, Petar Zaninovich, Trevor Ching, Vincent Kim 3 | 4 | This is a student project done for Oracle for CMPS 116/117 at UCSC. 5 | 6 | Oracle’s Graal Compiler and Truffle technologies allows multiple languages to freely interoperate. The goal of this project was to incorporate a subset of Go into Truffle’s suite of languages. 7 | 8 | Documentation on the subset completed can be found here: https://pdzaninov.github.io/GoLang-Compiler/ 9 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoContinueException.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.truffle.api.nodes.ControlFlowException; 4 | 5 | public final class GoContinueException extends ControlFlowException { 6 | 7 | public static final GoContinueException SINGLETON = new GoContinueException(); 8 | 9 | private static final long serialVersionUID = -91013036379258890L; 10 | 11 | private GoContinueException() {} 12 | } 13 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoLogicalNotNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoUnaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "!") 8 | public abstract class GoLogicalNotNode extends GoUnaryNode { 9 | 10 | @Specialization 11 | protected boolean doBoolean(boolean value) { 12 | return !value; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoBitwiseOrNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "|") 8 | public abstract class GoBitwiseOrNode extends GoBinaryNode{ 9 | 10 | @Specialization 11 | protected int bitwiseOr(int left, int right) { 12 | return left | right; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoBitwiseAndNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "&") 8 | public abstract class GoBitwiseAndNode extends GoBinaryNode{ 9 | 10 | @Specialization 11 | protected int bitwiseAnd(int left, int right) { 12 | return left & right; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoBinaryLeftShiftNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | 4 | import com.oracle.app.nodes.GoBinaryNode; 5 | import com.oracle.truffle.api.dsl.Specialization; 6 | import com.oracle.truffle.api.nodes.NodeInfo; 7 | 8 | @NodeInfo(shortName = "<<") 9 | public abstract class GoBinaryLeftShiftNode extends GoBinaryNode{ 10 | 11 | @Specialization 12 | protected int leftShift(int left, int right) { 13 | return left << right; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoBreakNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoStatementNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "break", description = "The node implementing a break statement") 8 | public final class GoBreakNode extends GoStatementNode { 9 | 10 | @Override 11 | public void executeVoid(VirtualFrame frame) { throw GoBreakException.SINGLETON; } 12 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoBitwiseXORNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | //shares same shortname as bitwise complement node 8 | @NodeInfo(shortName = "^") 9 | public abstract class GoBitwiseXORNode extends GoBinaryNode{ 10 | 11 | @Specialization 12 | protected int bitwiseXOR(int left, int right) { 13 | return left ^ right; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoContinueNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoStatementNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "continue", description = "The node implementing a continue statement") 8 | public final class GoContinueNode extends GoStatementNode { 9 | 10 | @Override 11 | public void executeVoid(VirtualFrame frame) { throw GoContinueException.SINGLETON; } 12 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRTypes.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | 5 | public abstract class GoIRTypes extends GoBaseIRNode { 6 | 7 | String type; 8 | String value; 9 | 10 | public GoIRTypes(String name, String type, String value) { 11 | super(name); 12 | this.type = type; 13 | } 14 | 15 | public String getValueType() { 16 | return type; 17 | } 18 | 19 | public String getValueString() { 20 | return value; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoBitwiseComplementNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoUnaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | //shares same shortname as bitwise XOR node 8 | @NodeInfo(shortName = "^") 9 | public abstract class GoBitwiseComplementNode extends GoUnaryNode{ 10 | 11 | @Specialization 12 | protected int bitwiseComplement(int value) { 13 | return ~value; 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/forward_decl.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | * Adds two variables with the result of vals(), vals() declared later in file 5 | */ 6 | func add(x int, y int) int { 7 | var a,b int = vals(4) 8 | x = x + a 9 | y = y + b 10 | return x + y 11 | } 12 | 13 | // Multiple returns 14 | func vals(a int) (int,int) { 15 | var c = something(a) 16 | return c,7 17 | } 18 | 19 | 20 | func something(a int) int { 21 | a = 4 22 | return a 23 | } 24 | 25 | 26 | func main() int { 27 | x := add(2, 3) 28 | return x 29 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoReturnException.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.truffle.api.nodes.ControlFlowException; 4 | 5 | public final class GoReturnException extends ControlFlowException { 6 | 7 | private static final long serialVersionUID = 4073191346281369231L; 8 | 9 | private final Object result; 10 | 11 | public GoReturnException(Object result) { 12 | this.result = result; 13 | } 14 | 15 | public Object getResult() { 16 | return result; 17 | } 18 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoBinaryRightShiftNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | //TODO Check if ints are the only thing that can be binary shifted 8 | @NodeInfo(shortName = ">>") 9 | public abstract class GoBinaryRightShiftNode extends GoBinaryNode{ 10 | 11 | @Specialization 12 | protected int rightShift(int left, int right) { 13 | return left >> right; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoArrayLikeTypes.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | public abstract class GoArrayLikeTypes extends GoNonPrimitiveType { 4 | 5 | //public abstract Object readArray(VirtualFrame frame, int index); 6 | 7 | public abstract GoPrimitiveTypes getType(); 8 | 9 | public abstract int len(); 10 | 11 | public abstract int cap(); 12 | 13 | public abstract int lowerBound(); 14 | 15 | public abstract Object read(Object index); 16 | 17 | public abstract void insert(Object index, Object value); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/GoDeleteBuiltin.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins; 2 | 3 | import com.oracle.app.nodes.types.FieldNode; 4 | import com.oracle.app.nodes.types.GoMap; 5 | import com.oracle.truffle.api.dsl.Specialization; 6 | import com.oracle.truffle.api.nodes.NodeInfo; 7 | 8 | @NodeInfo(shortName = "delete") 9 | public abstract class GoDeleteBuiltin extends GoBuiltinNode { 10 | 11 | @Specialization 12 | public GoMap deleteValue(GoMap map, Object value){ 13 | map.deleteElement(new FieldNode(value,null)); 14 | return map; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/runtime/GoNull.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.runtime; 2 | 3 | import com.oracle.truffle.api.interop.ForeignAccess; 4 | import com.oracle.truffle.api.interop.TruffleObject; 5 | 6 | public class GoNull implements TruffleObject{ 7 | 8 | public static final GoNull SINGLETON = new GoNull(); 9 | 10 | private GoNull() { 11 | } 12 | 13 | public String toString(){ 14 | return "nil"; 15 | } 16 | 17 | @Override 18 | public ForeignAccess getForeignAccess() { 19 | return GoNullMessageResolutionForeign.ACCESS; 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoBinaryNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.truffle.api.dsl.NodeChild; 4 | import com.oracle.truffle.api.dsl.NodeChildren; 5 | 6 | /** 7 | * Utility base class for operations that take two arguments (per convention called "left" and 8 | * "right"). For concrete subclasses of this class, the Truffle DSL creates two child fields, and 9 | * the necessary constructors and logic to set them. 10 | */ 11 | @NodeChildren({@NodeChild("leftNode"), @NodeChild("rightNode")}) 12 | public abstract class GoBinaryNode extends GoExpressionNode { 13 | } 14 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoNonPrimitiveType.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | 6 | /** 7 | * Objects that are not listed as a primitive type under {@link GoPrimitiveTypes} 8 | * fall under a non primitive type. Non primitive types can be initialized via Composite Lits 9 | * @author Trevor 10 | * 11 | */ 12 | public abstract class GoNonPrimitiveType extends GoExpressionNode { 13 | public abstract GoNonPrimitiveType doCompositeLit(VirtualFrame frame, Object[] results); 14 | } 15 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRDeclStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRDeclStmtNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode decl; 9 | 10 | public GoIRDeclStmtNode(GoBaseIRNode decl) { 11 | super("DeclStmt Node"); 12 | this.decl = decl; 13 | } 14 | 15 | @Override 16 | public Object accept(GoIRVisitor visitor) { 17 | return visitor.visitDeclStmt(this); 18 | } 19 | 20 | public GoBaseIRNode getChild() { 21 | return decl; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /golang/Assignment.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func vals(lol int, rofl int) (int,int,int){ 4 | x:=0 5 | return lol,rofl,x+6 6 | 7 | } 8 | 9 | func me(x int) int { 10 | return x 11 | 12 | } 13 | func sing(){ 14 | println("sing") 15 | } 16 | 17 | func main() { 18 | sing() 19 | var yoyo int = me(3) 20 | println(yoyo) 21 | var x,y,zzz = vals(5,yoyo) 22 | z,wtf, wtfz := vals(3,5) 23 | println(wtf + wtfz) 24 | println(x) 25 | println(y) 26 | println(z) 27 | println(zzz) 28 | var a,b = 1,2 29 | var c = 3 30 | println(a) 31 | println(b) 32 | println(c) 33 | d,f := 1,2 34 | g := 3 35 | println(d) 36 | println(f) 37 | println(g) 38 | println(me(3)) 39 | 40 | } -------------------------------------------------------------------------------- /golang/printast.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "go/ast" 5 | "go/parser" 6 | "go/token" 7 | "os" 8 | ) 9 | 10 | func check(e error) { 11 | if e != nil { 12 | panic(e) 13 | } 14 | } 15 | 16 | func main() { 17 | //TODO: add a way to write the files to a text 18 | files := os.Args[1:] 19 | // Create the AST by go src. 20 | fset := token.NewFileSet() 21 | for i := 0; i < len(files); i++ { 22 | f, err := parser.ParseFile(fset, string(files[i]), nil, parser.ParseComments) 23 | check(err) 24 | ast.Print(fset, f) 25 | // d1 := []byte(asts) 26 | // err := ioutil.WriteFile("/tmp/"+string(files[i]), d1, 0644) 27 | // check(err) 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/GoBaseIRNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir; 2 | 3 | import com.oracle.app.GoException; 4 | import com.oracle.truffle.api.source.Source; 5 | import com.oracle.truffle.api.source.SourceSection; 6 | 7 | public abstract class GoBaseIRNode implements GoIRVisitable { 8 | 9 | protected String name; 10 | 11 | public GoBaseIRNode(String name) { this.name = name; } 12 | public String toString() { return name; } 13 | public String getIdentifier(){ return name; } 14 | 15 | public abstract Object accept(GoIRVisitor visitor); 16 | 17 | public String TCself() { 18 | return ""; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRExprNode extends GoBaseIRNode { 7 | 8 | private GoIRArrayListExprNode child; 9 | 10 | public GoIRExprNode(GoIRArrayListExprNode child) { 11 | super("Expr"); 12 | this.child = child; 13 | } 14 | 15 | public GoIRArrayListExprNode getChild() { 16 | return child; 17 | } 18 | 19 | @Override 20 | public Object accept(GoIRVisitor visitor) { 21 | return visitor.visitExpr(this); 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFieldListNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRFieldListNode extends GoBaseIRNode { 7 | 8 | private GoIRArrayListExprNode fields; 9 | 10 | public GoIRFieldListNode(GoIRArrayListExprNode fields){ 11 | super("Field List node"); 12 | this.fields = fields; 13 | } 14 | 15 | public GoIRArrayListExprNode getFields() { return fields; } 16 | 17 | @Override 18 | public Object accept(GoIRVisitor visitor) { return visitor.visitFieldList(this); } 19 | 20 | 21 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoUndefinedFunctionRootNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.runtime.GoUndefinedNameException; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | import com.oracle.truffle.api.nodes.RootNode; 7 | 8 | public class GoUndefinedFunctionRootNode extends GoRootNode { 9 | public GoUndefinedFunctionRootNode(GoLanguage language, String name) { 10 | super(language,null,null, null, null, name); 11 | } 12 | 13 | @Override 14 | public Object execute(VirtualFrame frame) { 15 | throw GoUndefinedNameException.undefinedFunction(getName()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRArrayFieldNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import java.util.ArrayList; 4 | 5 | import com.oracle.app.parser.ir.GoBaseIRNode; 6 | import com.oracle.app.parser.ir.GoIRVisitor; 7 | 8 | public class GoIRArrayFieldNode extends GoBaseIRNode { 9 | 10 | ArrayList children; 11 | 12 | public GoIRArrayFieldNode(ArrayList children) { 13 | super("ArrayList Expression Node"); 14 | this.children = children; 15 | } 16 | 17 | @Override 18 | public Object accept(GoIRVisitor visitor){ 19 | return visitor.visitArrayField(this); 20 | } 21 | 22 | public int getSize(){ 23 | return children.size(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoDecNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoIncDecStmtNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "--") 8 | public abstract class GoDecNode extends GoIncDecStmtNode { 9 | 10 | @Specialization 11 | protected int decrement(int value) { 12 | return value--; 13 | } 14 | 15 | @Specialization 16 | protected float decrement(float value) { 17 | return value--; 18 | } 19 | 20 | @Specialization 21 | protected double decrement(double value) { 22 | return value--; 23 | } 24 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoIncNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoIncDecStmtNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "++") 8 | public abstract class GoIncNode extends GoIncDecStmtNode { 9 | 10 | @Specialization 11 | protected int increment(int value) { 12 | return value++; 13 | } 14 | 15 | @Specialization 16 | protected float increment(float value) { 17 | return value++; 18 | } 19 | 20 | @Specialization 21 | protected double increment(double value) { 22 | return value++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/interop/GoTypeToForeignNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.interop; 2 | 3 | import com.oracle.app.nodes.GoTypes; 4 | import com.oracle.truffle.api.dsl.Fallback; 5 | import com.oracle.truffle.api.dsl.Specialization; 6 | import com.oracle.truffle.api.dsl.TypeSystemReference; 7 | import com.oracle.truffle.api.nodes.Node; 8 | 9 | @TypeSystemReference(GoTypes.class) 10 | public abstract class GoTypeToForeignNode extends Node { 11 | 12 | public abstract Object executeConvert(Object value); 13 | 14 | @Specialization 15 | static long fromLong(long value){ 16 | return value; 17 | } 18 | 19 | @Fallback 20 | static Object identity(Object value){ 21 | return value; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRMapTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRMapTypeNode extends GoBaseIRNode{ 7 | 8 | GoBaseIRNode key; 9 | GoBaseIRNode value; 10 | 11 | public GoIRMapTypeNode(GoBaseIRNode key, GoBaseIRNode value){ 12 | super("Map type node"); 13 | this.key = key; 14 | this.value = value; 15 | } 16 | 17 | public GoBaseIRNode getKey() { return key; } 18 | 19 | public GoBaseIRNode getValue() { return value; } 20 | 21 | @Override 22 | public Object accept(GoIRVisitor visitor) { return visitor.visitMapType(this); } 23 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.truffle.api.frame.VirtualFrame; 4 | import com.oracle.truffle.api.nodes.NodeInfo; 5 | 6 | /** 7 | * Node holding a child node which holds the array of node arguments 8 | */ 9 | @NodeInfo(shortName = "Expr", description = "The node implementing a source code Expr") 10 | public final class GoExprNode extends GoExpressionNode { 11 | 12 | 13 | @Child private GoExpressionNode bodyNode; 14 | 15 | public GoExprNode(GoExpressionNode bodyNode) { 16 | this.bodyNode = bodyNode; 17 | } 18 | 19 | @Override 20 | public Object executeGeneric(VirtualFrame frame) { 21 | 22 | return bodyNode.executeGeneric(frame); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoLogicalOrNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.nodes.NodeInfo; 5 | 6 | /** 7 | * Logical disjunction node with short circuit evaluation. 8 | */ 9 | @NodeInfo(shortName = "||") 10 | public final class GoLogicalOrNode extends GoShortCircuitNode { 11 | 12 | public GoLogicalOrNode(GoExpressionNode left, GoExpressionNode right) { 13 | super(left, right); 14 | } 15 | 16 | @Override 17 | protected boolean isEvaluateRight(boolean left) { 18 | return !left; 19 | } 20 | 21 | @Override 22 | protected boolean execute(boolean left, boolean right) { 23 | return left || right; 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoNegativeSignNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoUnaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | //shares same shortname as sub node 8 | @NodeInfo(shortName = "-") 9 | public abstract class GoNegativeSignNode extends GoUnaryNode{ 10 | 11 | @Specialization 12 | protected int negativeSign(int value) { 13 | return 0 - value; 14 | } 15 | 16 | @Specialization 17 | protected float negativeSign(float value) { 18 | return 0 - value; 19 | } 20 | 21 | @Specialization 22 | protected double negativeSign(double value) { 23 | return 0 - value; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoPositiveSignNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoUnaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | //shares same shortname as add node 8 | @NodeInfo(shortName = "+") 9 | public abstract class GoPositiveSignNode extends GoUnaryNode{ 10 | 11 | @Specialization 12 | protected int positiveSign(int value) { 13 | return 0 + value; 14 | } 15 | 16 | @Specialization 17 | protected float positiveSign(float value) { 18 | return 0 + value; 19 | } 20 | 21 | @Specialization 22 | protected double positiveSign(double value) { 23 | return 0 + value; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/local/GoReadArgumentsNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.local; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.runtime.GoNull; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | 7 | public class GoReadArgumentsNode extends GoExpressionNode{ 8 | 9 | private final int index; 10 | 11 | //variable thing that does branch profiling 12 | 13 | public GoReadArgumentsNode(int index){ 14 | this.index = index; 15 | } 16 | 17 | @Override 18 | public Object executeGeneric(VirtualFrame frame) { 19 | Object[] args = frame.getArguments(); 20 | if(index < args.length){ 21 | return args[index]; 22 | } 23 | else{ 24 | return GoNull.SINGLETON; 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import java.util.ArrayList; 4 | 5 | import com.oracle.app.parser.ir.GoBaseIRNode; 6 | import com.oracle.app.parser.ir.GoIRVisitor; 7 | 8 | public class GoIRStmtNode extends GoBaseIRNode { 9 | 10 | private ArrayList children; 11 | 12 | public GoIRStmtNode(ArrayList children) { 13 | super("Stmt"); 14 | this.children = children; 15 | } 16 | 17 | public int getSize(){ 18 | return children.size(); 19 | } 20 | 21 | public ArrayList getChildren(){ 22 | return children; 23 | } 24 | 25 | @Override 26 | public Object accept(GoIRVisitor visitor) { 27 | return visitor.visitStmt(this); 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoStringNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.truffle.api.frame.VirtualFrame; 4 | 5 | public class GoStringNode extends GoPrimitiveType { 6 | public final String str; 7 | 8 | public GoStringNode(String str) { 9 | this.str = str; 10 | } 11 | 12 | @Override 13 | public String executeString(VirtualFrame frame) { 14 | return this.str; 15 | } 16 | 17 | @Override 18 | public Object executeGeneric(VirtualFrame frame) { 19 | return this.str; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "String Node " + str; 25 | } 26 | 27 | @Override 28 | public GoPrimitiveTypes getType() { 29 | return GoPrimitiveTypes.STRING; 30 | } 31 | 32 | 33 | 34 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoFloat64Node.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.truffle.api.frame.VirtualFrame; 4 | 5 | public class GoFloat64Node extends GoPrimitiveType { 6 | public final double number; 7 | 8 | public GoFloat64Node(double number) { 9 | this.number = number; 10 | } 11 | 12 | @Override 13 | public double executeDouble(VirtualFrame virtualFrame) { 14 | return this.number; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "" + this.number; 20 | } 21 | 22 | @Override 23 | public Object executeGeneric(VirtualFrame frame) { 24 | return this.number; 25 | } 26 | 27 | @Override 28 | public GoPrimitiveTypes getType() { 29 | return GoPrimitiveTypes.FLOAT64; 30 | } 31 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRStructTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoIRVisitor; 4 | 5 | public class GoIRStructTypeNode extends GoIRTypes { 6 | 7 | GoIRFieldListNode fieldList; 8 | boolean incomplete; 9 | 10 | public GoIRStructTypeNode(GoIRFieldListNode fieldList, String incomplete) { 11 | super("Struct Type node", "Struct", "StructVal"); 12 | this.fieldList = fieldList; 13 | this.incomplete = Boolean.getBoolean(incomplete); 14 | } 15 | 16 | public GoIRFieldListNode getFieldListNode() { return fieldList; } 17 | 18 | public boolean getIncomplete() { return incomplete; } 19 | 20 | @Override 21 | public Object accept(GoIRVisitor visitor) { 22 | return visitor.visitStructType(this); 23 | } 24 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoIntNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | 6 | public class GoIntNode extends GoPrimitiveType { 7 | public final int number; 8 | 9 | public GoIntNode(int number) { 10 | this.number = number; 11 | } 12 | 13 | @Override 14 | public int executeInteger(VirtualFrame virtualFrame) { 15 | return number; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Int Node " + number; 21 | } 22 | 23 | @Override 24 | public Object executeGeneric(VirtualFrame frame) { 25 | return number; 26 | } 27 | 28 | @Override 29 | public GoPrimitiveTypes getType() { 30 | return GoPrimitiveTypes.INT; 31 | } 32 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRSelectorExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRSelectorExprNode extends GoBaseIRNode { 7 | 8 | private GoBaseIRNode expr; 9 | private GoIRIdentNode name; 10 | 11 | public GoIRSelectorExprNode(GoBaseIRNode expr, GoIRIdentNode name) { 12 | super("SelectorExpr"); 13 | this.expr = expr; 14 | this.name = name; 15 | } 16 | 17 | public GoBaseIRNode getExpr() { 18 | return expr; 19 | } 20 | 21 | public GoIRIdentNode getName() { 22 | return name; 23 | } 24 | 25 | @Override 26 | public Object accept(GoIRVisitor visitor) { 27 | return visitor.visitSelectorExpr(this); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/runtime/GoUndefinedNameException.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.runtime; 2 | 3 | import com.oracle.app.GoException; 4 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 5 | 6 | public final class GoUndefinedNameException extends GoException { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | @TruffleBoundary 11 | public static GoUndefinedNameException undefinedFunction(Object name) { 12 | throw new GoUndefinedNameException("Undefined function: " + name); 13 | } 14 | 15 | @TruffleBoundary 16 | public static GoUndefinedNameException undefinedProperty(Object name) { 17 | throw new GoUndefinedNameException("Undefined property: " + name); 18 | } 19 | 20 | private GoUndefinedNameException(String message) { 21 | super(message); 22 | } 23 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoModNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | import com.oracle.app.nodes.GoBinaryNode; 3 | import com.oracle.truffle.api.dsl.Specialization; 4 | import com.oracle.truffle.api.nodes.NodeInfo; 5 | 6 | 7 | @NodeInfo(shortName = "%") 8 | public abstract class GoModNode extends GoBinaryNode { 9 | 10 | 11 | @Specialization(rewriteOn = ArithmeticException.class) 12 | protected int mod(int left, int right) { 13 | return left % right; 14 | } 15 | 16 | @Specialization(rewriteOn = ArithmeticException.class) 17 | protected float mod(float left, float right) { 18 | return left % right; 19 | } 20 | 21 | @Specialization(rewriteOn = ArithmeticException.class) 22 | protected double mod(double left, double right) { 23 | return left % right; 24 | } 25 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRImportSpecNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | import java.util.ArrayList; 7 | 8 | public class GoIRImportSpecNode extends GoBaseIRNode { 9 | 10 | private GoIRBasicLitNode child; 11 | 12 | public GoIRImportSpecNode(GoIRBasicLitNode child) { 13 | super("ImportSpec"); 14 | this.child = child; 15 | } 16 | 17 | @Override 18 | public String getIdentifier(){ 19 | return child.getIdentifier(); 20 | } 21 | 22 | public GoIRBasicLitNode getChild() { 23 | return child; 24 | } 25 | 26 | @Override 27 | public Object accept(GoIRVisitor visitor) { 28 | return visitor.visitImportSpec(this); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoFloat32Node.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | 6 | public class GoFloat32Node extends GoPrimitiveType { 7 | public final float number; 8 | 9 | public GoFloat32Node(float number) { 10 | this.number = number; 11 | } 12 | 13 | @Override 14 | public float executeFloat(VirtualFrame virtualFrame) { 15 | return this.number; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "" + this.number; 21 | } 22 | 23 | @Override 24 | public Object executeGeneric(VirtualFrame frame) { 25 | return this.number; 26 | } 27 | 28 | @Override 29 | public GoPrimitiveTypes getType() { 30 | return GoPrimitiveTypes.FLOAT32; 31 | } 32 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRIncDecStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRIncDecStmtNode extends GoBaseIRNode { 7 | 8 | String op; 9 | GoBaseIRNode child; 10 | String tokpos; 11 | 12 | public GoIRIncDecStmtNode(String op, GoBaseIRNode child, String tokpos) { 13 | super("IncDec"); 14 | this.op = op; 15 | this.child = child; 16 | this.tokpos = tokpos; 17 | } 18 | 19 | public GoBaseIRNode getChild() { 20 | return child; 21 | } 22 | 23 | public String getTokPos(){ 24 | return tokpos; 25 | } 26 | 27 | public String getOp() { 28 | return op; 29 | } 30 | 31 | @Override 32 | public Object accept(GoIRVisitor visitor) { 33 | return visitor.visitIncDecStmt(this); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/GoBuiltinNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.nodes.GoExpressionNode; 5 | import com.oracle.app.runtime.GoContext; 6 | import com.oracle.truffle.api.dsl.GenerateNodeFactory; 7 | import com.oracle.truffle.api.dsl.NodeChild; 8 | import com.oracle.truffle.api.dsl.NodeField; 9 | 10 | @NodeChild(value = "arguments", type = GoExpressionNode[].class) 11 | @GenerateNodeFactory 12 | public abstract class GoBuiltinNode extends GoExpressionNode { 13 | 14 | /** 15 | * Accessor for the {@link GoContext}. The implementation of this method is generated 16 | * automatically based on the {@link NodeField} annotation on the class. 17 | */ 18 | public final GoContext getContext() { 19 | return getRootNode().getLanguage(GoLanguage.class).getContextReference().get(); 20 | } 21 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/runtime/GoNullMessageResolution.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.runtime; 2 | 3 | import com.oracle.truffle.api.interop.CanResolve; 4 | import com.oracle.truffle.api.interop.MessageResolution; 5 | import com.oracle.truffle.api.interop.Resolve; 6 | import com.oracle.truffle.api.interop.TruffleObject; 7 | import com.oracle.truffle.api.nodes.Node; 8 | 9 | @MessageResolution(receiverType = GoNull.class) 10 | public class GoNullMessageResolution{ 11 | 12 | @Resolve(message = "IS_NULL") 13 | public abstract static class GoForeignIsNullNode extends Node{ 14 | public Object access(Object receiver){ 15 | return GoNull.SINGLETON == receiver; 16 | } 17 | } 18 | 19 | @CanResolve 20 | public abstract static class CheckNull extends Node{ 21 | protected static boolean test(TruffleObject receiver){ 22 | return receiver instanceof GoNull; 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRObjectNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitable; 5 | import com.oracle.app.parser.ir.GoIRVisitor; 6 | 7 | public class GoIRObjectNode extends GoBaseIRNode { 8 | 9 | GoBaseIRNode functionNode; 10 | String kind; 11 | 12 | public GoIRObjectNode(GoBaseIRNode functionNode, String kind) { 13 | super("Object Node"); 14 | this.functionNode = functionNode; 15 | this.kind = kind; 16 | } 17 | 18 | public GoBaseIRNode getFunctionNode() { 19 | return functionNode; 20 | } 21 | 22 | public String getKind() { 23 | return kind; 24 | } 25 | 26 | @Override 27 | public Object accept(GoIRVisitor visitor) { 28 | return visitor.visitObjectNode(this); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoLessThanNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | 8 | @NodeInfo(shortName = "<") 9 | public abstract class GoLessThanNode extends GoBinaryNode { 10 | 11 | @Specialization 12 | protected boolean lessThan(int left, int right) { 13 | return left < right; 14 | } 15 | 16 | @Specialization 17 | protected boolean lessThan(float left, float right) { 18 | return left < right; 19 | } 20 | 21 | @Specialization 22 | protected boolean lessThan(double left, double right) { 23 | return left < right; 24 | } 25 | 26 | @Specialization 27 | protected boolean lessThan(String left, String right) { 28 | return left.compareTo(right) < 0; 29 | } 30 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoLessOrEqualNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = "<=") 8 | public abstract class GoLessOrEqualNode extends GoBinaryNode { 9 | 10 | @Specialization 11 | protected boolean lessOrEqual(int left, int right) { 12 | return left <= right; 13 | } 14 | 15 | @Specialization 16 | protected boolean lessOrEqual(float left, float right) { 17 | return left <= right; 18 | } 19 | 20 | @Specialization 21 | protected boolean lessOrEqual(double left, double right) { 22 | return left <= right; 23 | } 24 | 25 | @Specialization 26 | protected boolean lessOrEqual(String left, String right) { 27 | return left.compareTo(right) <= 0; 28 | } 29 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/FieldNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.expression.GoKeyValueNode; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | 7 | public class FieldNode extends GoExpressionNode{ 8 | protected Object value; 9 | protected String type; 10 | 11 | public FieldNode(Object value, String type){ 12 | this.value = value; 13 | this.type = type; 14 | } 15 | 16 | @Override 17 | public String toString(){ 18 | return value.toString() + " "; 19 | } 20 | 21 | public void insert(Object value){ 22 | this.value = value; 23 | } 24 | 25 | public Object read(){ 26 | return this.value; 27 | } 28 | 29 | @Override 30 | public Object executeGeneric(VirtualFrame frame) { 31 | // TODO Auto-generated method stub 32 | return null; 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRBranchStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRBranchStmtNode extends GoBaseIRNode { 7 | 8 | String type; 9 | String source; 10 | GoBaseIRNode child; 11 | 12 | public GoIRBranchStmtNode(String type,GoBaseIRNode child, String source) { 13 | super("BranchStmt"); 14 | this.type = type; 15 | this.child = child; 16 | this.source = source; 17 | } 18 | 19 | public int getSourceLine(){ 20 | return Integer.parseInt(source.split(":")[1]); 21 | } 22 | 23 | public String getType() { 24 | return type; 25 | } 26 | 27 | public GoBaseIRNode getChild() { return child; } 28 | 29 | @Override 30 | public Object accept(GoIRVisitor visitor) { 31 | return visitor.visitBranchStmt(this); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFileNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRFileNode extends GoBaseIRNode { 7 | 8 | GoIRIdentNode name; 9 | GoBaseIRNode decls; 10 | GoBaseIRNode imports; 11 | 12 | public GoIRFileNode(GoIRIdentNode name, GoBaseIRNode decls, GoBaseIRNode imports) { 13 | super("IR File Node"); 14 | this.name = name; 15 | this.decls = decls; 16 | this.imports = imports; 17 | } 18 | 19 | public GoIRIdentNode getName() { 20 | return name; 21 | } 22 | 23 | public GoBaseIRNode getDecls() { 24 | return decls; 25 | } 26 | 27 | public GoBaseIRNode getImports() { 28 | return imports; 29 | } 30 | 31 | @Override 32 | public Object accept(GoIRVisitor visitor) { 33 | return visitor.visitFile(this); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoStruct.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.truffle.api.object.ObjectType; 4 | import com.oracle.truffle.api.object.Property; 5 | import com.oracle.truffle.api.object.Shape; 6 | 7 | /** 8 | * Structs can be modeled off of a simple truffle shape where properties are added during 9 | * type specs, then properties are only modified during selector expressions. 10 | * Still need to read up on how the Truffle Object library works and see if Layouts could be of use 11 | * @author Trevor 12 | * 13 | */ 14 | public class GoStruct extends ObjectType{ 15 | public static final ObjectType SINGLETON = new GoStruct(); 16 | public static final int STRUCT_FIELD = 0; 17 | public static final int STRUCT_METHOD = 1; 18 | 19 | private GoStruct(){ 20 | } 21 | 22 | public static Shape.Pred getKeyList(){ 23 | return p -> p.getFlags() == STRUCT_FIELD; 24 | } 25 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRExprStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRExprStmtNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode expression; 9 | String lparen; 10 | String rparen; 11 | 12 | public GoIRExprStmtNode(GoBaseIRNode expression, String lparen, String rparen) { 13 | super("Parenthesis Expression"); 14 | this.expression = expression; 15 | this.lparen = lparen; 16 | this.rparen = rparen; 17 | } 18 | public GoIRExprStmtNode(GoBaseIRNode expression) { 19 | super("Stand alone Expression"); 20 | this.expression = expression; 21 | } 22 | 23 | public GoBaseIRNode getChild() { 24 | return expression; 25 | } 26 | 27 | @Override 28 | public Object accept(GoIRVisitor visitor){ 29 | return visitor.visitExprStmt(this); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoGreaterOrEqualNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | @NodeInfo(shortName = ">=") 8 | public abstract class GoGreaterOrEqualNode extends GoBinaryNode { 9 | 10 | @Specialization 11 | protected boolean greaterOrEqual(int left, int right) { 12 | return left >= right; 13 | } 14 | 15 | @Specialization 16 | protected boolean greaterOrEqual(float left, float right) { 17 | return left >= right; 18 | } 19 | 20 | @Specialization 21 | protected boolean greaterOrEqual(double left, double right) { 22 | return left >= right; 23 | } 24 | 25 | @Specialization 26 | protected boolean greaterOrEqual(String left, String right) { 27 | return left.compareTo(right) >= 0; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoMulNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoBinaryNode; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | /** 8 | * This class is similar to the extensively documented {@link GoAddNode}. 9 | */ 10 | @NodeInfo(shortName = "*") 11 | public abstract class GoMulNode extends GoBinaryNode { 12 | 13 | @Specialization(rewriteOn = ArithmeticException.class) 14 | protected int mul(int left, int right) { 15 | return Math.multiplyExact(left, right); 16 | } 17 | 18 | @Specialization(rewriteOn = ArithmeticException.class) 19 | protected float mul(float left, float right) { 20 | return left * right; 21 | } 22 | 23 | @Specialization(rewriteOn = ArithmeticException.class) 24 | protected double mul(double left, double right) { 25 | return left * right; 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoBooleanNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | 6 | public class GoBooleanNode extends GoPrimitiveType { 7 | public final boolean value; 8 | 9 | public GoBooleanNode(boolean bool) { 10 | this.value = bool; 11 | } 12 | 13 | @Override 14 | public boolean executeBoolean(VirtualFrame virtualFrame) { 15 | return this.value; 16 | } 17 | 18 | 19 | @Override 20 | public String toString() { 21 | if (this.value) { 22 | return "true"; 23 | } else { 24 | return "false"; 25 | } 26 | } 27 | 28 | @Override 29 | public Object executeGeneric(VirtualFrame frame) { 30 | return this.value; 31 | } 32 | 33 | @Override 34 | public GoPrimitiveTypes getType() { 35 | return GoPrimitiveTypes.BOOL; 36 | } 37 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoMapTypeExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoIdentNode; 5 | import com.oracle.app.nodes.call.GoFieldNode; 6 | import com.oracle.app.nodes.local.GoReadLocalVariableNode; 7 | import com.oracle.app.nodes.types.FieldNode; 8 | import com.oracle.app.nodes.types.GoMap; 9 | import com.oracle.truffle.api.frame.VirtualFrame; 10 | 11 | public class GoMapTypeExprNode extends GoExpressionNode{ 12 | GoExpressionNode keyType; 13 | GoExpressionNode valueType; 14 | 15 | public GoMapTypeExprNode(GoExpressionNode keyType, GoExpressionNode valueType){ 16 | this.keyType = keyType; 17 | this.valueType = valueType; 18 | } 19 | 20 | public Object executeGeneric(VirtualFrame frame){ 21 | 22 | GoMap result = new GoMap(keyType.executeGeneric(frame), valueType.executeGeneric(frame)); 23 | return result; 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/types/GoFunctionLiteralNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.types; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.nodes.GoExpressionNode; 5 | import com.oracle.app.runtime.GoContext; 6 | import com.oracle.truffle.api.TruffleLanguage.ContextReference; 7 | import com.oracle.truffle.api.frame.VirtualFrame; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | 10 | @NodeInfo(shortName = "func") 11 | public final class GoFunctionLiteralNode extends GoExpressionNode { 12 | 13 | private final String functionName; 14 | 15 | private final ContextReference reference; 16 | 17 | public GoFunctionLiteralNode(GoLanguage language, String functionName) { 18 | this.functionName = functionName; 19 | this.reference = language.getContextReference(); 20 | } 21 | 22 | @Override 23 | public Object executeGeneric(VirtualFrame frame){ 24 | return reference.get().getFunctionRegistry().lookup(functionName, true); 25 | } 26 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRKeyValueNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRKeyValueNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode key; 9 | String colon; 10 | GoBaseIRNode value; 11 | 12 | public GoIRKeyValueNode(GoBaseIRNode key, String colon, GoBaseIRNode value) { 13 | super("Key Value Expression Node"); 14 | this.key = key; 15 | this.colon = colon; 16 | this.value = value; 17 | } 18 | 19 | @Override 20 | public Object accept(GoIRVisitor visitor) { 21 | return visitor.visitKeyValue(this); 22 | } 23 | 24 | @Override 25 | public String getIdentifier(){ 26 | return key.getIdentifier(); 27 | } 28 | 29 | public GoBaseIRNode getKey() { 30 | return key; 31 | } 32 | 33 | public String getColon() { 34 | return colon; 35 | } 36 | 37 | public GoBaseIRNode getValue() { 38 | return value; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRValueSpecNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRValueSpecNode extends GoBaseIRNode { 7 | 8 | private GoIRArrayListExprNode names; 9 | private GoBaseIRNode type; 10 | private GoIRArrayListExprNode expr; 11 | 12 | public GoIRValueSpecNode(GoIRArrayListExprNode names, GoBaseIRNode type, GoIRArrayListExprNode expr) { 13 | super("Value Spec Node"); 14 | this.names = names; 15 | this.type = type; 16 | this.expr = expr; 17 | } 18 | 19 | public GoIRArrayListExprNode getNames(){ 20 | return names; 21 | } 22 | 23 | public GoIRArrayListExprNode getExpr(){ 24 | return expr; 25 | } 26 | 27 | public GoBaseIRNode getType(){ 28 | return type; 29 | } 30 | 31 | @Override 32 | public Object accept(GoIRVisitor visitor) { 33 | return null; 34 | //return visitor.visitValueSpec(this); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFieldNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRFieldNode extends GoBaseIRNode { 7 | 8 | private GoIRArrayListExprNode names; 9 | private GoBaseIRNode type; 10 | 11 | public GoIRFieldNode(String name,GoIRArrayListExprNode names, GoBaseIRNode type) { 12 | super(name); 13 | this.names = names; 14 | this.type = type; 15 | } 16 | 17 | public GoIRFieldNode(String name, GoBaseIRNode type) { 18 | super(name); 19 | this.type = type; 20 | } 21 | 22 | public GoIRArrayListExprNode getNames() { 23 | return names; 24 | } 25 | 26 | public GoBaseIRNode getType() { 27 | return type; 28 | } 29 | 30 | public String getTypeName() { 31 | if(type != null) { 32 | return type.getIdentifier(); 33 | } 34 | return null; 35 | } 36 | 37 | @Override 38 | public Object accept(GoIRVisitor visitor){ 39 | return visitor.visitField(this); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/call/GoFieldNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.call; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.local.GoReadLocalVariableNode; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | 7 | public class GoFieldNode extends GoExpressionNode{ 8 | 9 | private final String name; 10 | private final GoReadLocalVariableNode type; 11 | 12 | public GoFieldNode(String names, GoReadLocalVariableNode type) { 13 | this.name = names; 14 | this.type = type; 15 | } 16 | 17 | @Override 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public GoReadLocalVariableNode getType() { 23 | return type; 24 | } 25 | 26 | @Override 27 | public Object executeGeneric(VirtualFrame frame) { 28 | /*TODO see if i can delete 29 | if(name != null) { 30 | FrameDescriptor frameDescriptor = frame.getFrameDescriptor(); 31 | FrameSlot slot = frameDescriptor.findFrameSlot(name); 32 | frame.setObject(slot, frame.getArguments()[0]); 33 | }*/ 34 | return null; 35 | } 36 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoGreaterThanNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import java.math.BigInteger; 4 | 5 | import com.oracle.app.nodes.GoBinaryNode; 6 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 7 | import com.oracle.truffle.api.dsl.Specialization; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | 10 | @NodeInfo(shortName = ">") 11 | public abstract class GoGreaterThanNode extends GoBinaryNode{ 12 | 13 | @Specialization 14 | protected boolean greaterThan(int left, int right) { 15 | return left > right; 16 | } 17 | 18 | @Specialization 19 | protected boolean greaterThan(float left, float right) { 20 | return left > right; 21 | } 22 | 23 | @Specialization 24 | protected boolean greaterThan(double left, double right) { 25 | return left > right; 26 | } 27 | 28 | @Specialization 29 | protected boolean greaterThan(String left, String right) { 30 | return left.compareTo(right) > 0; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRStarNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRStarNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode expr; 9 | String star; 10 | 11 | public GoIRStarNode(GoBaseIRNode expr, String star) { 12 | super("Star Node"); 13 | this.expr = expr; 14 | this.star = star; 15 | } 16 | 17 | public GoBaseIRNode getChild(){ 18 | return expr; 19 | } 20 | 21 | public int getStarLineNum(){ 22 | String[] split = star.split(":"); 23 | return Integer.parseInt(split[1]); 24 | } 25 | 26 | public int getStarColumn(){ 27 | String[] split = star.split(":"); 28 | return Integer.parseInt(split[2]); 29 | } 30 | 31 | @Override 32 | public String getIdentifier() { 33 | return expr.getIdentifier(); 34 | } 35 | 36 | @Override 37 | public Object accept(GoIRVisitor visitor) { 38 | return visitor.visitStarNode(this); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoSubNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import java.math.BigInteger; 4 | 5 | import com.oracle.app.nodes.GoBinaryNode; 6 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 7 | import com.oracle.truffle.api.dsl.Specialization; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | 10 | /** 11 | * This class is similar to the extensively documented {@link SLAddNode}. 12 | */ 13 | @NodeInfo(shortName = "-") 14 | public abstract class GoSubNode extends GoBinaryNode { 15 | 16 | @Specialization(rewriteOn = ArithmeticException.class) 17 | protected int sub(int left, int right) { 18 | return Math.subtractExact(left, right); 19 | } 20 | 21 | @Specialization(rewriteOn = ArithmeticException.class) 22 | protected float sub(float left, float right) { 23 | return left - right; 24 | } 25 | 26 | @Specialization(rewriteOn = ArithmeticException.class) 27 | protected double sub(double left, double right) { 28 | return left - right; 29 | } 30 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/local/GoReadPropertyNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.local; 2 | 3 | import com.oracle.app.GoException; 4 | import com.oracle.app.runtime.GoContext; 5 | import com.oracle.truffle.api.CompilerAsserts; 6 | import com.oracle.truffle.api.dsl.Specialization; 7 | import com.oracle.truffle.api.nodes.Node; 8 | import com.oracle.truffle.api.object.DynamicObject; 9 | 10 | public abstract class GoReadPropertyNode extends Node { 11 | 12 | public abstract Object executeRead(Object receiver, Object name); 13 | 14 | @Specialization(guards = "isStruct(receiver)") 15 | public Object doRead(DynamicObject receiver, Object name){ 16 | Object result = receiver.get(name); 17 | if(result == null){ 18 | throw new GoException("undefined field "+ name); 19 | } 20 | return result; 21 | } 22 | 23 | public boolean isStruct(DynamicObject receiver){ 24 | CompilerAsserts.neverPartOfCompilation(); 25 | if(!GoContext.isGoStruct(receiver)){ 26 | return false; 27 | } 28 | return receiver.getShape().isValid(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoIdentNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.app.GoException; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | 6 | /* 7 | * Technically if an ident shows up in runtime, that is an undefined variable 8 | */ 9 | public class GoIdentNode extends GoExpressionNode{ 10 | 11 | String name; 12 | @Child private GoExpressionNode child; 13 | 14 | public GoIdentNode(String name, GoExpressionNode child) { 15 | this.name = name; 16 | this.child = child; 17 | } 18 | 19 | public void setChild(GoExpressionNode child) { 20 | this.child = child; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return name; 26 | } 27 | 28 | @Override 29 | public String getName(){ 30 | return name; 31 | } 32 | 33 | /** 34 | * Child only executes when it has a function declared in it. 35 | */ 36 | @Override 37 | public Object executeGeneric(VirtualFrame frame) { 38 | if(child != null) { 39 | return child.executeGeneric(frame); 40 | } 41 | throw new GoException("Undefined: "+ name); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRTypeSpecNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | //Not sure if Gen Decl handles type spec yet since we do not have a spec node. It seems to be array list expression node. 7 | 8 | public class GoIRTypeSpecNode extends GoBaseIRNode { 9 | 10 | private GoBaseIRNode name; 11 | private GoBaseIRNode type; 12 | 13 | public GoIRTypeSpecNode(GoBaseIRNode name, GoBaseIRNode type){ 14 | super("Type Spec Node"); 15 | this.name = name; 16 | this.type = type; 17 | } 18 | 19 | @Override 20 | public String getIdentifier(){ 21 | return name.getIdentifier(); 22 | } 23 | 24 | public GoBaseIRNode getName() { return name; } 25 | 26 | public GoBaseIRNode getType() { return type; } 27 | 28 | public String getTypeName() { return type.getIdentifier(); } 29 | 30 | @Override 31 | public Object accept(GoIRVisitor visitor) { 32 | return visitor.visitTypeSpec(this); 33 | } 34 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRBlockStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRBlockStmtNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode body; 9 | String lbrace; 10 | String rbrace; 11 | 12 | public GoIRBlockStmtNode(GoBaseIRNode body,String lbrace, String rbrace) { 13 | super("Block Statement Node"); 14 | this.body = body; 15 | this.lbrace = lbrace; 16 | this.rbrace = rbrace; 17 | } 18 | 19 | public GoBaseIRNode getChild() { 20 | return body; 21 | } 22 | 23 | public int getLbrace(){ 24 | String[] split = lbrace.split(":"); 25 | int lbraceindex = Integer.parseInt(split[2]); 26 | return lbraceindex; 27 | } 28 | 29 | public int getRbrace(){ 30 | String[] split = rbrace.split(":"); 31 | int endindex = Integer.parseInt(split[2]); 32 | return endindex; 33 | } 34 | 35 | @Override 36 | public Object accept(GoIRVisitor visitor){ 37 | return visitor.visitBlockStmt(this); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoLogicalAndNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.nodes.NodeInfo; 5 | /** 6 | * Logical conjunction node with short circuit evaluation. 7 | */ 8 | @NodeInfo(shortName = "&&") 9 | public final class GoLogicalAndNode extends GoShortCircuitNode { 10 | 11 | public GoLogicalAndNode(GoExpressionNode left, GoExpressionNode right) { 12 | super(left, right); 13 | } 14 | 15 | /** 16 | * The right value does not need to be evaluated if the left value is already false 17 | * . 18 | */ 19 | @Override 20 | protected boolean isEvaluateRight(boolean left) { 21 | return left; 22 | } 23 | 24 | /** 25 | * Only if left and right value are true the result of the logical and is true. If 26 | * the second parameter is not evaluated, false is provided. 27 | */ 28 | @Override 29 | protected boolean execute(boolean left, boolean right) { 30 | return left && right; 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRArrayTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRArrayTypeNode extends GoIRTypes { 7 | GoBaseIRNode len; 8 | GoBaseIRNode type; 9 | String source; 10 | 11 | public GoIRArrayTypeNode(GoBaseIRNode length, GoBaseIRNode type, String source) { 12 | super("IR Array Type Node", type.getIdentifier(), type.getIdentifier()); 13 | len = length; 14 | this.type = type; 15 | this.source = source; 16 | } 17 | 18 | public int getLBrackStartLine(){ 19 | String[] split = source.split(":"); 20 | return Integer.parseInt(split[1]); 21 | } 22 | 23 | public int getLBrackStartColumn(){ 24 | String[] split = source.split(":"); 25 | return Integer.parseInt(split[2]); 26 | } 27 | 28 | public GoBaseIRNode getLength(){ 29 | return len; 30 | } 31 | 32 | public GoBaseIRNode getType(){ 33 | return type; 34 | } 35 | 36 | @Override 37 | public Object accept(GoIRVisitor visitor) { 38 | return visitor.visitArrayType(this); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoArrayExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.truffle.api.frame.VirtualFrame; 4 | 5 | public class GoArrayExprNode extends GoExpressionNode { 6 | 7 | @Children final private GoExpressionNode[] children; 8 | 9 | public GoArrayExprNode(GoExpressionNode[] children) { 10 | this.children = children; 11 | } 12 | 13 | public GoExpressionNode[] getArguments(){ 14 | return children; 15 | } 16 | 17 | public int getSize() { 18 | return children.length; 19 | } 20 | 21 | 22 | @Override 23 | public Object executeGeneric(VirtualFrame frame) { 24 | for(GoExpressionNode child : children){ 25 | child.executeGeneric(frame); 26 | } 27 | return null; 28 | } 29 | 30 | /* 31 | * Called by composite lit node. Gathers the results to initialize a non primitive type. 32 | */ 33 | public Object[] gatherResults(VirtualFrame frame){ 34 | Object[] results = new Object[children.length]; 35 | for(int i = 0; i < results.length; i++){ 36 | results[i] = children[i].executeGeneric(frame); 37 | } 38 | return results; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRForNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRForNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode init; 9 | GoBaseIRNode cond; 10 | GoBaseIRNode post; 11 | GoBaseIRNode body; 12 | String fortok; 13 | 14 | public GoIRForNode(GoBaseIRNode init, GoBaseIRNode cond, GoBaseIRNode post, GoBaseIRNode body,String fortok) { 15 | super("For Loop"); 16 | this.init = init; 17 | this.cond = cond; 18 | this.post = post; 19 | this.body = body; 20 | this.fortok = fortok; 21 | } 22 | 23 | public int getSourceLine(){ 24 | return Integer.parseInt(fortok.split(":")[1]); 25 | } 26 | 27 | public GoBaseIRNode getInit() { return init; } 28 | 29 | public GoBaseIRNode getCond() { return cond; } 30 | 31 | public GoBaseIRNode getPost() { return post; } 32 | 33 | public GoBaseIRNode getBody() { return body; } 34 | 35 | @Override 36 | public Object accept(GoIRVisitor visitor) { 37 | return visitor.visitForLoop(this); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRUnaryNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRUnaryNode extends GoBaseIRNode { 7 | 8 | String op; 9 | String source; 10 | GoBaseIRNode child; 11 | 12 | public GoIRUnaryNode(String op, GoBaseIRNode child,String source) { 13 | super("Unary"); 14 | this.op = op; 15 | this.child = child; 16 | this.source = source; 17 | } 18 | 19 | public GoBaseIRNode getChild() { 20 | return child; 21 | } 22 | 23 | public int getOpTokLineNum(){ 24 | String[] split = source.split(":"); 25 | return Integer.parseInt(split[1]); 26 | } 27 | 28 | public int getOptTokStartCol(){ 29 | String[] split = source.split(":"); 30 | return Integer.parseInt(split[2]); 31 | } 32 | 33 | public String getOp() { 34 | return op; 35 | } 36 | 37 | @Override 38 | public Object accept(GoIRVisitor visitor) { 39 | return visitor.visitUnary(this); 40 | } 41 | 42 | public String TCself() { 43 | return child.TCself(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/fmt/GoFmtPrintln.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins.fmt; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | import com.oracle.truffle.api.nodes.NodeInfo; 7 | 8 | @NodeInfo(shortName = "Println") 9 | public class GoFmtPrintln extends GoExpressionNode { 10 | 11 | @Override 12 | public Object executeGeneric(VirtualFrame frame) { 13 | if(frame.getArguments().length < 1){ 14 | return null; 15 | } 16 | Object[] arguments = frame.getArguments(); 17 | StringBuilder builder = new StringBuilder(); 18 | for(int i = 0; i < arguments.length; i++){ 19 | builder.append(arguments[i].toString() +" "); 20 | } 21 | if(builder.length() > 1){ 22 | builder.deleteCharAt(builder.length()-1); 23 | } 24 | doPrint(builder); 25 | return null; 26 | } 27 | 28 | @TruffleBoundary 29 | private void doPrint(StringBuilder builder) { 30 | System.out.println(builder); 31 | } 32 | 33 | public static GoFmtPrintln getFmtPrintln(){ 34 | return new GoFmtPrintln(); 35 | } 36 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFuncTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRFuncTypeNode extends GoBaseIRNode { 7 | private GoBaseIRNode params; 8 | private GoBaseIRNode results; 9 | private String funcpos; 10 | 11 | public GoIRFuncTypeNode(GoBaseIRNode params, GoBaseIRNode results, String funcpos) { 12 | super("FuncType"); 13 | this.params = params; 14 | this.results = results; 15 | this.funcpos = funcpos; 16 | } 17 | 18 | public int getFuncLinePos(){ 19 | String[] split = funcpos.split(":"); 20 | return Integer.parseInt(split[1]); 21 | } 22 | 23 | public int getFuncStartColumn(){ 24 | String[] split = funcpos.split(":"); 25 | return Integer.parseInt(split[2]); 26 | } 27 | 28 | public GoBaseIRNode getParams() { 29 | return params; 30 | } 31 | 32 | public GoBaseIRNode getResults() { 33 | return results; 34 | } 35 | 36 | @Override 37 | public Object accept(GoIRVisitor visitor){ 38 | return visitor.visitFuncType(this); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoSwitchNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoStatementNode; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | import com.oracle.truffle.api.nodes.NodeInfo; 7 | 8 | 9 | @NodeInfo(shortName = "switch", description = "A node implementing a switch statement") 10 | public final class GoSwitchNode extends GoStatementNode { 11 | 12 | @Child private GoStatementNode init; 13 | @Child private GoExpressionNode tag; 14 | @Child private GoBlockNode body; 15 | 16 | public GoSwitchNode(GoStatementNode init, GoExpressionNode tag, GoBlockNode body){ 17 | this.init = init; 18 | this.tag = tag; 19 | this.body = body; 20 | } 21 | 22 | @Override 23 | public void executeVoid(VirtualFrame frame){ 24 | if(init != null){ 25 | init.executeVoid(frame); 26 | } 27 | if (tag != null) { 28 | Object value = tag.executeGeneric(frame); 29 | body.switchExecute(frame, value); 30 | } else { 31 | body.ifElseExecute(frame); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoKeyValueNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | 6 | //TODO ?????? I feel like the way the results our executed is wonky 7 | public class GoKeyValueNode extends GoExpressionNode { 8 | 9 | private GoExpressionNode key; 10 | private GoExpressionNode value; 11 | private Object result; 12 | private Object keyResult; 13 | 14 | public GoKeyValueNode(GoExpressionNode key, GoExpressionNode value) { 15 | this.key = key; 16 | this.value = value; 17 | result = null; 18 | keyResult = null; 19 | } 20 | 21 | @Override 22 | public Object executeGeneric(VirtualFrame frame) { 23 | result = value.executeGeneric(frame); 24 | keyResult = key.getName(); 25 | return this; 26 | } 27 | 28 | public GoExpressionNode getKey() { 29 | return key; 30 | } 31 | 32 | public GoExpressionNode getValue() { 33 | return value; 34 | } 35 | 36 | public Object getResult() { 37 | return result; 38 | } 39 | 40 | public Object getKeyResult() { return keyResult; } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/call/GoFieldListNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.call; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | // probably should extend something else 6 | public class GoFieldListNode extends GoExpressionNode { 7 | 8 | @Children GoFieldNode[] nodes; 9 | 10 | 11 | @Override 12 | public Object executeGeneric(VirtualFrame frame) { 13 | // TODO Auto-generated method stub 14 | return null; 15 | } 16 | 17 | 18 | 19 | 20 | 21 | 22 | } 23 | //A FieldList represents a list of Fields, enclosed by parentheses or braces. 24 | // 25 | //type FieldList struct { 26 | // Opening token.Pos // position of opening parenthesis/brace, if any 27 | // List []*Field // field list; or nil 28 | // Closing token.Pos // position of closing parenthesis/brace, if any 29 | //} 30 | //func (*FieldList) End 31 | //func (f *FieldList) End() token.Pos 32 | //func (*FieldList) NumFields 33 | //func (f *FieldList) NumFields() int 34 | //NumFields returns the number of (named and anonymous fields) in a FieldList. 35 | // 36 | //func (*FieldList) Pos 37 | //func (f *FieldList) Pos() token.Pos -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRReturnStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRReturnStmtNode extends GoBaseIRNode{ 7 | GoIRArrayListExprNode children; 8 | int index; 9 | String returnpos; 10 | 11 | public int getIndex() { 12 | return index; 13 | } 14 | 15 | public void setIndex(int i) { 16 | index = i; 17 | } 18 | 19 | public GoIRReturnStmtNode(GoIRArrayListExprNode children, String returnpos) { 20 | super("ArrayList Expression Node"); 21 | this.children = children; 22 | this.returnpos = returnpos; 23 | } 24 | 25 | @Override 26 | public Object accept(GoIRVisitor visitor){ 27 | return visitor.visitReturnStmt(this); 28 | } 29 | 30 | public int getReturnPosLineNum(){ 31 | String[] split = returnpos.split(":"); 32 | return Integer.parseInt(split[1]); 33 | } 34 | 35 | public int getReturnPosStartColumn(){ 36 | String[] split = returnpos.split(":"); 37 | return Integer.parseInt(split[2]); 38 | } 39 | 40 | public GoIRArrayListExprNode getChild() { 41 | return children; 42 | 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoStarExpressionNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.local.GoReadLocalVariableNode; 5 | import com.oracle.app.nodes.types.GoPointerNode; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | 8 | /** 9 | * Should only be called on {@link GoReadLocalVariableNode}s that contain 10 | * a pointer in them. This is only used to print the value a pointer is pointing 11 | * at. Did not see a way to directly execute the pointer object without calling a 12 | * specific method inside {@link GoPointerNode} 13 | * @author Trevor 14 | * 15 | */ 16 | public class GoStarExpressionNode extends GoExpressionNode { 17 | 18 | private GoExpressionNode ptr; 19 | 20 | public GoStarExpressionNode(GoExpressionNode ptr){ 21 | this.ptr = ptr; 22 | } 23 | 24 | @Override 25 | public Object executeGeneric(VirtualFrame frame) { 26 | //GoPointerNode node = (GoPointerNode) FrameUtil.getObjectSafe(frame, ptr.getSlot()); 27 | GoPointerNode node = (GoPointerNode) ptr.executeGeneric(frame); 28 | return node.executeStar(frame); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRGenDeclNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | import com.oracle.truffle.api.source.Source; 6 | 7 | public class GoIRGenDeclNode extends GoBaseIRNode { 8 | 9 | String token; 10 | GoIRArrayListExprNode children; 11 | String tokpos; 12 | String lparen; 13 | String rparen; 14 | 15 | public GoIRGenDeclNode(String token, GoIRArrayListExprNode children,String tokpos, String lparen, String rparen) { 16 | super("GenDecl Node"); 17 | this.token = token; 18 | this.children = children; 19 | this.tokpos = tokpos; 20 | this.lparen = lparen; 21 | this.rparen = rparen; 22 | } 23 | 24 | public GoIRArrayListExprNode getChild(){ 25 | return children; 26 | } 27 | 28 | public String getToken(){ 29 | return token; 30 | } 31 | 32 | public int getTokPos(){ 33 | String[] split = tokpos.split(":"); 34 | int tokindex = Integer.parseInt(split[2]); 35 | return tokindex; 36 | } 37 | 38 | @Override 39 | public Object accept(GoIRVisitor visitor) { 40 | return visitor.visitGenDecl(this); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRSwitchStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRSwitchStmtNode extends GoBaseIRNode { 7 | 8 | GoIRStmtNode init; 9 | GoBaseIRNode tag; //Ident node in .ast file but in ast documentation is Expr Node 10 | GoIRBlockStmtNode body; 11 | String source; 12 | 13 | public GoIRSwitchStmtNode(GoIRStmtNode init, GoBaseIRNode tag, GoIRBlockStmtNode body,String source) { 14 | super("SwitchStmt"); 15 | this.init = init; 16 | this.tag = tag; 17 | this.body = body; 18 | this.source = source; 19 | } 20 | 21 | public int getSourceLine(){ 22 | return Integer.parseInt(source.split(":")[1]); 23 | } 24 | 25 | public GoIRStmtNode getInit(){ 26 | return init; 27 | } 28 | 29 | public GoBaseIRNode getTag(){ 30 | return tag; 31 | } 32 | 33 | public GoIRBlockStmtNode getBody(){ 34 | return body; 35 | } 36 | 37 | @Override 38 | public Object accept(GoIRVisitor visitor) { 39 | return visitor.visitSwitchStmt(this); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/PointerTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.oracle.truffle.api.source.Source; 10 | import com.oracle.truffle.api.vm.PolyglotEngine; 11 | 12 | public class PointerTest { 13 | private PolyglotEngine engine; 14 | private PolyglotEngine.Value math; 15 | 16 | @Before 17 | public void setUp() throws Exception{ 18 | engine = PolyglotEngine.newBuilder().build(); 19 | Source source = Source.newBuilder("" 20 | +"package main\n" 21 | + "\n" 22 | + "func main() int{\n" 23 | + " a := 5 \n" 24 | + " ptr := &a \n" 25 | + " *ptr = 10 \n" 26 | + " return a \n" 27 | + "}"). 28 | name("UnitTest.go"). 29 | mimeType("text/x-go").build(); 30 | ToolChain.executeCommands(source); 31 | engine.eval(source); 32 | math = engine.findGlobalSymbol("main"); 33 | } 34 | 35 | @After 36 | public void tearDown(){ 37 | engine.dispose(); 38 | } 39 | 40 | @Test 41 | public void test(){ 42 | Number ret = math.execute().as(Number.class); 43 | assertEquals(10,ret.intValue()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRAssignmentStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRAssignmentStmtNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode lhs; 9 | GoBaseIRNode rhs; 10 | GoBaseIRNode type; 11 | String tokpos; 12 | 13 | public GoIRAssignmentStmtNode(GoBaseIRNode lhs, GoBaseIRNode rhs, GoBaseIRNode type, String tokpos){ 14 | super("Assignment Statement Node"); 15 | this.lhs = lhs; 16 | this.rhs = rhs; 17 | this.type = type; 18 | this.tokpos = tokpos; 19 | } 20 | 21 | public int getTokPosLineNum(){ 22 | String[] split = tokpos.split(":"); 23 | return Integer.parseInt(split[1]); 24 | } 25 | 26 | @Override 27 | public String getIdentifier(){ 28 | return lhs.getIdentifier(); 29 | } 30 | 31 | public GoBaseIRNode getLHS(){ 32 | return lhs; 33 | } 34 | 35 | public GoBaseIRNode getRHS(){ 36 | return rhs; 37 | } 38 | 39 | public GoBaseIRNode getType() { 40 | return type; 41 | } 42 | 43 | @Override 44 | public Object accept(GoIRVisitor visitor) { 45 | return visitor.visitAssignment(this); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRBinaryExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.GoException; 4 | import com.oracle.app.parser.ir.GoBaseIRNode; 5 | import com.oracle.app.parser.ir.GoIRVisitor; 6 | import com.oracle.truffle.api.source.Source; 7 | import com.oracle.truffle.api.source.SourceSection; 8 | 9 | public class GoIRBinaryExprNode extends GoBaseIRNode { 10 | 11 | String op; 12 | GoBaseIRNode left; 13 | GoBaseIRNode right; 14 | String source; 15 | String type = null; 16 | 17 | public GoIRBinaryExprNode(String op, GoBaseIRNode left, GoBaseIRNode right, String source) { 18 | super("BinaryExpr"); 19 | this.op = op; 20 | this.left = left; 21 | this.right = right; 22 | this.source = source; 23 | } 24 | 25 | public GoBaseIRNode getLeft() { return left; } 26 | 27 | public GoBaseIRNode getRight() { return right; } 28 | 29 | public String getOp() { 30 | return op; 31 | } 32 | 33 | @Override 34 | public Object accept(GoIRVisitor visitor) { 35 | return visitor.visitBinaryExpr(this); 36 | } 37 | 38 | public String getType() { 39 | return type; 40 | } 41 | 42 | public void setType(String a) { 43 | type = a; 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRIfStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRIfStmtNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode init; 9 | GoBaseIRNode cond; 10 | GoBaseIRNode body; 11 | GoBaseIRNode elsenode; 12 | String iftok; 13 | 14 | public GoIRIfStmtNode(GoBaseIRNode init, GoBaseIRNode cond, GoBaseIRNode body, GoBaseIRNode elsenode,String iftok) { 15 | super("If Node"); 16 | this.init=init; 17 | this.cond=cond; 18 | this.body=body; 19 | this.elsenode=elsenode; 20 | this.iftok = iftok; 21 | } 22 | 23 | public int getSourceLine(){ 24 | return Integer.parseInt(iftok.split(":")[1]); 25 | } 26 | 27 | public GoBaseIRNode getInit() { 28 | return init; 29 | } 30 | 31 | public GoBaseIRNode getCond() { 32 | return cond; 33 | } 34 | 35 | public GoBaseIRNode getBody() { 36 | return body; 37 | } 38 | 39 | public GoBaseIRNode getElse() { 40 | return elsenode; 41 | } 42 | 43 | @Override 44 | public Object accept(GoIRVisitor visitor) { 45 | return visitor.visitIfStmt(this); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRCaseClauseNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRCaseClauseNode extends GoBaseIRNode { 7 | 8 | GoIRArrayListExprNode list; 9 | GoIRStmtNode body; 10 | String casetok; 11 | String colon; 12 | 13 | public GoIRCaseClauseNode(GoIRArrayListExprNode list, GoIRStmtNode body,String casetok, String colon) { 14 | super("CaseClause"); 15 | this.list = list; 16 | this.body = body; 17 | this.casetok = casetok; 18 | this.colon = colon; 19 | } 20 | 21 | public int getSourceLine(){ 22 | return Integer.parseInt(casetok.split(":")[1]); 23 | } 24 | 25 | public int getCaseStart(){ 26 | return Integer.parseInt(casetok.split(":")[2]); 27 | } 28 | 29 | public int getSourceLength(){ 30 | return Integer.parseInt(colon.split(":")[2]); 31 | } 32 | 33 | public GoIRStmtNode getBody() { 34 | return body; 35 | } 36 | 37 | public GoIRArrayListExprNode getList() { 38 | return list; 39 | } 40 | 41 | @Override 42 | public Object accept(GoIRVisitor visitor) { 43 | return visitor.visitCaseClause(this); 44 | } 45 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoTypes.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.app.nodes.types.GoArray; 4 | import com.oracle.app.nodes.types.GoMap; 5 | import com.oracle.app.nodes.types.GoPointerNode; 6 | import com.oracle.app.nodes.types.GoSlice; 7 | import com.oracle.app.nodes.types.GoStruct; 8 | import com.oracle.app.runtime.GoFunction; 9 | import com.oracle.app.runtime.GoNull; 10 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 11 | import com.oracle.truffle.api.dsl.ImplicitCast; 12 | import com.oracle.truffle.api.dsl.TypeSystem; 13 | 14 | @TypeSystem({ 15 | int.class, 16 | float.class, 17 | double.class, 18 | boolean.class, 19 | GoArray.class, 20 | GoSlice.class, 21 | GoMap.class, 22 | String.class, 23 | GoFunction.class, 24 | GoPointerNode.class, 25 | GoStruct.class, 26 | GoNull.class}) 27 | public abstract class GoTypes { 28 | 29 | @ImplicitCast 30 | @TruffleBoundary 31 | public static float castFloat(int value){ 32 | return (float)value; 33 | } 34 | 35 | @ImplicitCast 36 | @TruffleBoundary 37 | public static double castDouble(int value){ 38 | return (double)value; 39 | } 40 | 41 | @ImplicitCast 42 | @TruffleBoundary 43 | public static double castDouble(float value){ 44 | return (double)value; 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoTempIRNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import com.oracle.app.parser.ir.GoBaseIRNode; 8 | import com.oracle.app.parser.ir.GoIRVisitor; 9 | 10 | public class GoTempIRNode extends GoBaseIRNode { 11 | 12 | Map attrs = new HashMap<>(); 13 | Map children; 14 | GoBaseIRNode parent; 15 | 16 | public GoTempIRNode(String name, Map attributes, Map childs) { 17 | super(name); 18 | 19 | attrs = attributes; 20 | children = childs; 21 | setChildParent(); 22 | } 23 | 24 | public void setParent(GoBaseIRNode node) { 25 | this.parent = node; 26 | } 27 | 28 | public void setChildParent() { 29 | if(children.isEmpty()){ 30 | for(GoBaseIRNode child : children.values()){ 31 | ((GoTempIRNode) child).setParent(this); 32 | } 33 | } 34 | 35 | } 36 | 37 | public ArrayList getChildren() { 38 | ArrayList temp = new ArrayList<>(); 39 | for(GoBaseIRNode child : children.values()){ 40 | temp.add(child); 41 | } 42 | return temp; 43 | 44 | } 45 | 46 | public Object accept(GoIRVisitor visitor) { 47 | return visitor.visitObject(this); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/SimpleMathTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.oracle.truffle.api.source.Source; 10 | import com.oracle.truffle.api.vm.PolyglotEngine; 11 | 12 | public class SimpleMathTest { 13 | 14 | private PolyglotEngine engine; 15 | private PolyglotEngine.Value math; 16 | 17 | @Before 18 | public void setUp() throws Exception { 19 | engine = PolyglotEngine.newBuilder().build(); 20 | Source source = Source.newBuilder("" 21 | +"package main\n" 22 | +"\n" 23 | +"func main() int{\n" 24 | +" var a, n int = 1, 5 \n" 25 | +" for x:= 1; x <= n; x++ {\n" 26 | +" a *= x\n" 27 | +" }\n" 28 | +" return a \n" 29 | +"}"). 30 | name("UnitTest.go"). 31 | mimeType("text/x-go"). 32 | build(); 33 | ToolChain.executeCommands(source); 34 | engine.eval(source); 35 | math = engine.findGlobalSymbol("main"); 36 | } 37 | 38 | @After 39 | public void tearDown() throws Exception { 40 | engine.dispose(); 41 | } 42 | 43 | @Test 44 | public void test() throws Exception{ 45 | 46 | Number ret = math.execute().as(Number.class); 47 | assertEquals(120,ret.intValue()); 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRCompositeLitNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRCompositeLitNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode expr; 9 | String lbrace; 10 | GoIRArrayListExprNode elts; 11 | String rbrace; 12 | 13 | public GoIRCompositeLitNode(GoBaseIRNode expr, String lbrace, GoIRArrayListExprNode elts, String rbrace) { 14 | super("Composite Lit Node"); 15 | this.expr = expr; 16 | this.lbrace = lbrace; 17 | this.elts = elts; 18 | this.rbrace = rbrace; 19 | } 20 | 21 | @Override 22 | public Object accept(GoIRVisitor visitor) { 23 | return visitor.visit(this); 24 | } 25 | 26 | public GoBaseIRNode getExpr() { 27 | return expr; 28 | } 29 | 30 | public int getLBraceLineNum() { 31 | String[] split = lbrace.split(":"); 32 | return Integer.parseInt(split[1]); 33 | } 34 | 35 | public int getLBraceStartColumn(){ 36 | String[] split = lbrace.split(":"); 37 | return Integer.parseInt(split[2]); 38 | } 39 | 40 | public GoIRArrayListExprNode getElts() { 41 | return elts; 42 | } 43 | 44 | public int getRBraceEndColumn() { 45 | String[] split = rbrace.split(":"); 46 | return Integer.parseInt(split[2]); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/DeclarationsTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.*; 4 | import java.io.File; 5 | import java.io.IOException; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.oracle.truffle.api.source.Source; 12 | import com.oracle.truffle.api.vm.PolyglotEngine; 13 | 14 | public class DeclarationsTest { 15 | 16 | private PolyglotEngine engine; 17 | private PolyglotEngine.Value math; 18 | private File goSource; 19 | String filename = "declarations"; 20 | 21 | @Before 22 | public void setUp() throws IOException, InterruptedException{ 23 | engine = PolyglotEngine.newBuilder().build(); 24 | goSource = new File("src/test/java/com/oracle/app/declarations.go"); 25 | Source source = Source.newBuilder(goSource).build(); 26 | ToolChain.executeCommands(source); 27 | engine.eval(source); 28 | math = engine.findGlobalSymbol("main"); 29 | } 30 | 31 | @After 32 | public void tearDown() throws IOException, InterruptedException{ 33 | Runtime rt = Runtime.getRuntime(); 34 | Process p = rt.exec("rm " + filename + ".go " + filename + ".ast"); 35 | p.waitFor(); 36 | engine.dispose(); 37 | } 38 | 39 | @Test 40 | public void test() { 41 | Number ret = math.execute().as(Number.class); 42 | assertEquals(42,ret.intValue()); 43 | } 44 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/SpecDecl/GoImportSpec.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.SpecDecl; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.builtins.fmt.FmtFunctionList; 5 | import com.oracle.app.nodes.GoExpressionNode; 6 | import com.oracle.app.nodes.types.GoStringNode; 7 | import com.oracle.truffle.api.frame.FrameSlot; 8 | import com.oracle.truffle.api.frame.FrameSlotKind; 9 | import com.oracle.truffle.api.frame.VirtualFrame; 10 | 11 | /** 12 | * Load the built in library when executed 13 | * Will currently not handle user defined file imports 14 | * @author Trevor 15 | * 16 | */ 17 | public class GoImportSpec extends GoExpressionNode { 18 | 19 | @Child private GoStringNode child; 20 | private final GoLanguage language; 21 | private final FrameSlot slot; 22 | 23 | public GoImportSpec(GoStringNode child, GoLanguage language, FrameSlot slot) { 24 | this.child = child; 25 | this.language = language; 26 | this.slot = slot; 27 | } 28 | 29 | @Override 30 | public Object executeGeneric(VirtualFrame frame) { 31 | String name = (String) child.executeGeneric(frame); 32 | slot.setKind(FrameSlotKind.Object); 33 | switch(name) { 34 | case "fmt": 35 | frame.setObject(slot, new FmtFunctionList(language)); 36 | break; 37 | default: 38 | System.out.println("Package not yet done"); 39 | } 40 | return null; 41 | } 42 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/GlobalVariableTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.*; 4 | import java.io.File; 5 | import java.io.IOException; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.oracle.truffle.api.source.Source; 12 | import com.oracle.truffle.api.vm.PolyglotEngine; 13 | 14 | public class GlobalVariableTest { 15 | 16 | private PolyglotEngine engine; 17 | private PolyglotEngine.Value math; 18 | private File goSource; 19 | String filename = "global_variable"; 20 | 21 | @Before 22 | public void setUp() throws IOException, InterruptedException{ 23 | engine = PolyglotEngine.newBuilder().build(); 24 | goSource = new File("src/test/java/com/oracle/app/global_variable.go"); 25 | Source source = Source.newBuilder(goSource).build(); 26 | ToolChain.executeCommands(source); 27 | engine.eval(source); 28 | math = engine.findGlobalSymbol("main"); 29 | } 30 | 31 | @After 32 | public void tearDown() throws IOException, InterruptedException{ 33 | Runtime rt = Runtime.getRuntime(); 34 | Process p = rt.exec("rm " + filename + ".go " + filename + ".ast"); 35 | p.waitFor(); 36 | engine.dispose(); 37 | } 38 | 39 | @Test 40 | public void test() { 41 | Number ret = math.execute().as(Number.class); 42 | assertEquals(7,ret.intValue()); 43 | } 44 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/SpecDecl/GoSelectorExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.SpecDecl; 2 | 3 | import com.oracle.app.builtins.fmt.FmtFunctionList; 4 | import com.oracle.app.nodes.GoExpressionNode; 5 | import com.oracle.app.nodes.local.GoReadPropertyNode; 6 | import com.oracle.app.nodes.local.GoReadPropertyNodeGen; 7 | import com.oracle.app.nodes.types.GoStringNode; 8 | import com.oracle.truffle.api.dsl.NodeChild; 9 | import com.oracle.truffle.api.dsl.NodeChildren; 10 | import com.oracle.truffle.api.dsl.Specialization; 11 | import com.oracle.truffle.api.frame.VirtualFrame; 12 | import com.oracle.truffle.api.object.DynamicObject; 13 | 14 | @NodeChildren({@NodeChild(value="varname"),@NodeChild(value="field",type=GoStringNode.class)}) 15 | public abstract class GoSelectorExprNode extends GoExpressionNode { 16 | 17 | public abstract GoExpressionNode getVarname(); 18 | 19 | @Specialization 20 | public Object executeStruct(DynamicObject struct, String field){ 21 | GoReadPropertyNode property = GoReadPropertyNodeGen.create(); 22 | return property.doRead(struct, field); 23 | } 24 | 25 | @Specialization 26 | public Object executeImport(FmtFunctionList imports, String function){ 27 | return imports.getFunction(function); 28 | } 29 | 30 | public Object getSelector(VirtualFrame frame){ 31 | return getVarname().executeGeneric(frame); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFuncDeclNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRFuncDeclNode extends GoBaseIRNode { 7 | 8 | 9 | GoBaseIRNode receiver; 10 | GoBaseIRNode name; 11 | GoBaseIRNode type; 12 | GoBaseIRNode body; 13 | 14 | public GoIRFuncDeclNode(GoBaseIRNode receiver, GoBaseIRNode name, GoBaseIRNode type, GoBaseIRNode body) { 15 | super("Function Declaration Node"); 16 | this.receiver = receiver; 17 | this.name = name; 18 | this.type = type; 19 | this.body = body; 20 | } 21 | 22 | @Override 23 | public Object accept(GoIRVisitor visitor){ 24 | return visitor.visitFuncDecl(this); 25 | } 26 | 27 | public GoBaseIRNode getName(){ 28 | return name; 29 | } 30 | 31 | @Override 32 | public String getIdentifier() { 33 | return name.getIdentifier(); 34 | } 35 | 36 | public boolean isReceiver(){ 37 | return receiver != null; 38 | } 39 | 40 | public GoBaseIRNode getReceiver(){ 41 | if(isReceiver()){ 42 | return receiver; 43 | } 44 | return null; 45 | } 46 | 47 | public GoBaseIRNode getBody(){ 48 | if(body != null){ 49 | return body; 50 | } 51 | return null; 52 | } 53 | 54 | public GoBaseIRNode getType(){ 55 | return type; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/ForwardFunctionDeclarationTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.*; 4 | import java.io.File; 5 | import java.io.IOException; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.oracle.truffle.api.source.Source; 12 | import com.oracle.truffle.api.vm.PolyglotEngine; 13 | 14 | public class ForwardFunctionDeclarationTest { 15 | 16 | private PolyglotEngine engine; 17 | private PolyglotEngine.Value math; 18 | private File goSource; 19 | String filename = "forward_decl"; 20 | 21 | @Before 22 | public void setUp() throws IOException, InterruptedException{ 23 | engine = PolyglotEngine.newBuilder().build(); 24 | goSource = new File("src/test/java/com/oracle/app/forward_decl.go"); 25 | Source source = Source.newBuilder(goSource).build(); 26 | ToolChain.executeCommands(source); 27 | engine.eval(source); 28 | math = engine.findGlobalSymbol("main"); 29 | } 30 | 31 | @After 32 | public void tearDown() throws IOException, InterruptedException{ 33 | Runtime rt = Runtime.getRuntime(); 34 | Process p = rt.exec("rm " + filename + ".go " + filename + ".ast"); 35 | p.waitFor(); 36 | engine.dispose(); 37 | } 38 | 39 | @Test 40 | public void test() { 41 | Number ret = math.execute().as(Number.class); 42 | assertEquals(16,ret.intValue()); 43 | } 44 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/LexicalScopeTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.*; 4 | import java.io.File; 5 | import java.io.IOException; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.oracle.truffle.api.source.Source; 12 | import com.oracle.truffle.api.vm.PolyglotEngine; 13 | 14 | public class LexicalScopeTest { 15 | 16 | private PolyglotEngine engine; 17 | private PolyglotEngine.Value math; 18 | private File goSource; 19 | String filename = "lexical_scope_same_ident"; 20 | 21 | @Before 22 | public void setUp() throws IOException, InterruptedException{ 23 | engine = PolyglotEngine.newBuilder().build(); 24 | goSource = new File("src/test/java/com/oracle/app/lexical_scope_same_ident.go"); 25 | Source source = Source.newBuilder(goSource).build(); 26 | ToolChain.executeCommands(source); 27 | engine.eval(source); 28 | math = engine.findGlobalSymbol("main"); 29 | } 30 | 31 | @After 32 | public void tearDown() throws IOException, InterruptedException{ 33 | Runtime rt = Runtime.getRuntime(); 34 | Process p = rt.exec("rm " + filename + ".go " + filename + ".ast"); 35 | p.waitFor(); 36 | engine.dispose(); 37 | } 38 | 39 | @Test 40 | public void test() { 41 | Number ret = math.execute().as(Number.class); 42 | assertEquals(2,ret.intValue()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFloat32Node.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoIRVisitor; 4 | import com.oracle.truffle.api.source.Source; 5 | import com.oracle.truffle.api.source.SourceSection; 6 | 7 | public class GoIRFloat32Node extends GoIRBasicLitNode{ 8 | 9 | private float value; 10 | private int valuelen; 11 | 12 | public GoIRFloat32Node(String value, String source) { 13 | super(source, "FLOAT32", value); 14 | this.valuelen = value.length(); 15 | this.value = Float.valueOf(value); 16 | this.type = "FLOAT32"; 17 | } 18 | 19 | public GoIRFloat32Node(float value){ 20 | super("Float32 Node", "FLOAT32", Float.toString(value)); 21 | this.value = value; 22 | } 23 | 24 | public float getValue(){ 25 | return value; 26 | } 27 | 28 | 29 | @Override 30 | public Object accept(GoIRVisitor visitor) { 31 | return visitor.visitIRFloat32Node(this); 32 | } 33 | 34 | @Override 35 | public SourceSection getSource(Source section) { 36 | String[] split = source.split(":"); 37 | int linenum = Integer.parseInt(split[1]); 38 | int charindex = Integer.parseInt(split[2]); 39 | return section.createSection(linenum,charindex,valuelen); 40 | } 41 | 42 | @Override 43 | public String getValString() { 44 | // TODO Auto-generated method stub 45 | return Float.toString(value); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRFloat64Node.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoIRVisitor; 4 | import com.oracle.truffle.api.source.Source; 5 | import com.oracle.truffle.api.source.SourceSection; 6 | 7 | public class GoIRFloat64Node extends GoIRBasicLitNode{ 8 | 9 | private double value; 10 | private int valuelen; 11 | 12 | public GoIRFloat64Node(String value, String source) { 13 | super(source, "FLOAT64", value); 14 | this.valuelen = value.length(); 15 | this.value = Double.valueOf(value); 16 | this.type = "FLOAT64"; 17 | } 18 | 19 | public GoIRFloat64Node(double value){ 20 | super("Float64 Node", "FLOAT64", Double.toString(value)); 21 | this.value = value; 22 | } 23 | 24 | public double getValue(){ 25 | return value; 26 | } 27 | 28 | @Override 29 | public Object accept(GoIRVisitor visitor) { 30 | return visitor.visitIRFloat64Node(this); 31 | } 32 | 33 | @Override 34 | public SourceSection getSource(Source section) { 35 | String[] split = source.split(":"); 36 | int linenum = Integer.parseInt(split[1]); 37 | int charindex = Integer.parseInt(split[2]); 38 | return section.createSection(linenum,charindex,valuelen); 39 | } 40 | 41 | @Override 42 | public String getValString() { 43 | // TODO Auto-generated method stub 44 | return Double.toString(value); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/call/GoFuncTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.call; 2 | 3 | import java.util.Arrays; 4 | 5 | import com.oracle.app.nodes.GoArrayExprNode; 6 | import com.oracle.app.nodes.GoExpressionNode; 7 | import com.oracle.app.nodes.local.GoWriteLocalVariableNode; 8 | import com.oracle.truffle.api.frame.VirtualFrame; 9 | 10 | public class GoFuncTypeNode extends GoExpressionNode{ 11 | 12 | GoArrayExprNode params; 13 | String[] results; 14 | 15 | String type; 16 | 17 | public GoFuncTypeNode(GoArrayExprNode params, String[] results2) { 18 | this.params = params; 19 | this.results = results2; 20 | } 21 | 22 | public void appendReceiverStruct(GoWriteLocalVariableNode receiver){ 23 | GoExpressionNode[] newparams; 24 | if(params != null){ 25 | int oldsize = params.getSize(); 26 | newparams = Arrays.copyOf(params.getArguments(), oldsize + 1); 27 | newparams[oldsize] = receiver; 28 | } 29 | else{ 30 | newparams = new GoExpressionNode[1]; 31 | newparams[0] = receiver; 32 | } 33 | params = new GoArrayExprNode(newparams); 34 | } 35 | 36 | public GoArrayExprNode getParams() { 37 | return params; 38 | } 39 | 40 | public String[] getResults() { 41 | return results; 42 | } 43 | 44 | @Override 45 | public Object executeGeneric(VirtualFrame frame) { 46 | if(params == null) { 47 | return null; 48 | } 49 | return params.executeGeneric(frame); 50 | } 51 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRIntNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoIRVisitor; 4 | import com.oracle.truffle.api.source.Source; 5 | import com.oracle.truffle.api.source.SourceSection; 6 | 7 | public class GoIRIntNode extends GoIRBasicLitNode{ 8 | 9 | int value; 10 | int valuelen; 11 | 12 | public GoIRIntNode(String value, String source) { 13 | super(source, "INT", value); 14 | this.value = Integer.parseInt(value); 15 | valuelen = value.length(); 16 | this.type = "INT"; 17 | } 18 | 19 | public GoIRIntNode(int value,String source){ 20 | super("IR Int Node", "INT", Integer.toString(value)); 21 | this.value = value; 22 | valuelen = 1; 23 | this.source = source; 24 | } 25 | 26 | public int getValue(){ 27 | return value; 28 | } 29 | 30 | @Override 31 | public Object accept(GoIRVisitor visitor) { 32 | return visitor.visitIRIntNode(this); 33 | } 34 | 35 | @Override 36 | public SourceSection getSource(Source section) { 37 | String[] split = source.split(":"); 38 | int linenum = Integer.parseInt(split[1]); 39 | int charindex = Integer.parseInt(split[2]); 40 | return section.createSection(linenum,charindex,valuelen); 41 | } 42 | 43 | @Override 44 | public String getValString() { 45 | // TODO Auto-generated method stub 46 | return Integer.toString(value); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoForNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoStatementNode; 5 | import com.oracle.truffle.api.Truffle; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | import com.oracle.truffle.api.nodes.LoopNode; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | import com.oracle.truffle.api.source.SourceSection; 10 | 11 | @NodeInfo(shortName = "for", description = "The node implementing a for loop") 12 | public class GoForNode extends GoStatementNode { 13 | 14 | @Child private GoExpressionNode init; 15 | 16 | @Child private LoopNode loopNode; 17 | 18 | public GoForNode(GoExpressionNode init, GoExpressionNode conditionNode, GoExpressionNode post, GoStatementNode bodyNode) { 19 | this.init = init; 20 | if(post != null) 21 | this.loopNode = Truffle.getRuntime().createLoopNode(new GoForRepeatingNode(conditionNode,post,bodyNode)); 22 | else if (conditionNode != null) 23 | this.loopNode = Truffle.getRuntime().createLoopNode(new GoForWhileNode(conditionNode,bodyNode)); 24 | else 25 | this.loopNode = Truffle.getRuntime().createLoopNode(new GoForBreakNode(bodyNode)); 26 | } 27 | 28 | @Override 29 | public void executeVoid(VirtualFrame frame) { 30 | if(init != null) 31 | init.executeVoid(frame); 32 | loopNode.executeLoop(frame); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoParenExpressionNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 7 | 8 | /** 9 | * A {@link GoExpressionNode} that represents a parenthesized expression; it simply returns the 10 | * value of the enclosed (child) expression. It is represented separately in the AST for the purpose 11 | * of correct source attribution; this preserves the lexical relationship between the two 12 | * parentheses and allows a tool to describe the expression as distinct from its contents. 13 | * TODO This is probably useless. When parsing through gotruffle return the child instead of this node. 14 | */ 15 | @NodeInfo(description = "A parenthesized expression") 16 | public class GoParenExpressionNode extends GoExpressionNode { 17 | 18 | @Child private GoExpressionNode expression; 19 | 20 | public GoParenExpressionNode(GoExpressionNode expression) { 21 | this.expression = expression; 22 | } 23 | 24 | @Override 25 | public Object executeGeneric(VirtualFrame frame) { 26 | return expression.executeGeneric(frame); 27 | } 28 | 29 | @Override 30 | public boolean executeBoolean(VirtualFrame frame) throws UnexpectedResultException { 31 | return expression.executeBoolean(frame); 32 | } 33 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRStringNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoIRVisitor; 4 | import com.oracle.app.parser.ir.StringEscape; 5 | import com.oracle.truffle.api.source.Source; 6 | import com.oracle.truffle.api.source.SourceSection; 7 | 8 | public class GoIRStringNode extends GoIRBasicLitNode{ 9 | private String value; 10 | public GoIRStringNode(String value, String source) { 11 | super(source, "STRING", value); 12 | if(value.length() > 2){ 13 | value = value.substring(2, value.length()-2); 14 | value = value.replace("\\\\", "\\"); 15 | 16 | value = StringEscape.unescape_perl_string(value); 17 | } 18 | this.value = value; 19 | this.type = "STRING"; 20 | } 21 | 22 | public String getValue(){ 23 | return value; 24 | } 25 | 26 | @Override 27 | public Object accept(GoIRVisitor visitor) { 28 | return visitor.visitIRStringNode(this); 29 | } 30 | 31 | @Override 32 | public SourceSection getSource(Source section) { 33 | String[] split = source.split(":"); 34 | int linenum = Integer.parseInt(split[1]); 35 | int charindex = Integer.parseInt(split[2]); 36 | return section.createSection(linenum,charindex,value.length()); 37 | } 38 | 39 | @Override 40 | public String getValString() { 41 | // TODO Auto-generated method stub 42 | System.out.println("String val:" + value); 43 | return value; 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRIndexNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRIndexNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode name; 9 | GoBaseIRNode index; 10 | String lbrack; 11 | String rbrack; 12 | 13 | public GoIRIndexNode(GoBaseIRNode name, GoBaseIRNode index, String lbrack, String rbrack) { 14 | super("Index Node"); 15 | this.name = name; 16 | this.index = index; 17 | this.lbrack = lbrack; 18 | this.rbrack = rbrack; 19 | } 20 | 21 | public GoBaseIRNode getName(){ 22 | return name; 23 | } 24 | 25 | @Override 26 | public String getIdentifier(){ 27 | return name.getIdentifier(); 28 | } 29 | 30 | public GoBaseIRNode getIndex(){ 31 | return index; 32 | } 33 | 34 | public int getLineNumber(){ 35 | return Integer.parseInt(rbrack.split(":")[1]); 36 | } 37 | 38 | public int getRBrack(){ 39 | return Integer.parseInt(rbrack.split(":")[2]); 40 | } 41 | 42 | public int getLBrack(){ 43 | return Integer.parseInt(lbrack.split(":")[2]); 44 | } 45 | 46 | public int getSourceSize(){ 47 | int start = Integer.parseInt(lbrack.split(":")[2]); 48 | int end = Integer.parseInt(rbrack.split(":")[2]); 49 | return end - start; 50 | } 51 | 52 | @Override 53 | public Object accept(GoIRVisitor visitor) { 54 | return visitor.visitIndexNode(this); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoAddNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | import com.oracle.app.nodes.GoBinaryNode; 3 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 4 | import com.oracle.truffle.api.dsl.Specialization; 5 | import com.oracle.truffle.api.nodes.NodeInfo; 6 | 7 | /** 8 | * Go does not concatenate strings with other objects so there is no default type 9 | * as that allows for the wrong result. 10 | * @author Trevor 11 | * 12 | */ 13 | @NodeInfo(shortName = "+") 14 | public abstract class GoAddNode extends GoBinaryNode { 15 | 16 | @Specialization(rewriteOn = ArithmeticException.class) 17 | protected int add(int left, int right) { 18 | return Math.addExact(left, right); 19 | } 20 | 21 | @Specialization(rewriteOn = ArithmeticException.class) 22 | protected float add(float left, float right) { 23 | return left + right; 24 | } 25 | 26 | @Specialization(rewriteOn = ArithmeticException.class) 27 | protected double add(double left, double right) { 28 | return left + right; 29 | } 30 | 31 | @Specialization(guards = "isString(left, right)") 32 | @TruffleBoundary 33 | protected String add(String left, String right) { 34 | return left + right; 35 | } 36 | 37 | protected boolean isString(Object a, Object b) { 38 | return a instanceof String || b instanceof String; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "Add Node"; 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRArrayListExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import java.util.ArrayList; 4 | 5 | import com.oracle.app.parser.ir.GoBaseIRNode; 6 | import com.oracle.app.parser.ir.GoIRVisitor; 7 | 8 | public class GoIRArrayListExprNode extends GoBaseIRNode { 9 | 10 | ArrayList children; 11 | String source; 12 | 13 | public GoIRArrayListExprNode(ArrayList children,String source) { 14 | super("ArrayList Expression Node"); 15 | this.children = children; 16 | this.source = source; 17 | } 18 | 19 | public GoIRArrayListExprNode(ArrayList children) { 20 | super("ArrayList Expression Node"); 21 | this.children = children; 22 | this.source = null; 23 | } 24 | 25 | @Override 26 | public Object accept(GoIRVisitor visitor){ 27 | return visitor.visitArrayListExpr(this); 28 | } 29 | 30 | public void printChildren() { 31 | for(GoBaseIRNode child : children) { 32 | System.out.println("printing children of GoIRArrayListExprNode: " + child.toString()); 33 | } 34 | } 35 | 36 | public int getSize(){ 37 | return children.size(); 38 | } 39 | 40 | public ArrayList getChildren() { 41 | 42 | return children; 43 | } 44 | 45 | public String TCself() { 46 | String a = ""; 47 | for (int i = 0; i cap){ 25 | throw new GoException("len larger than cap in make()"); 26 | } 27 | return slice.make(len, cap); 28 | } 29 | 30 | @Specialization 31 | public GoSlice makeSlice(GoSlice slice, int len, GoNull x){ 32 | return slice.make(len, len); 33 | } 34 | 35 | @Specialization 36 | public GoMap makeMapWithCapacity(GoMap map, int len, GoNull x){ 37 | map.setNewMapCapacity(len); 38 | return map; 39 | } 40 | 41 | @Specialization 42 | public GoMap makeMap(GoMap map, GoNull x, GoNull y){ 43 | return map; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoUnaryAddressNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoUnaryNode; 5 | import com.oracle.app.nodes.types.GoArrayLikeTypes; 6 | import com.oracle.app.nodes.types.GoPointerNode; 7 | import com.oracle.app.nodes.types.GoPointerNode.GoArrayIndexPointerNode; 8 | import com.oracle.truffle.api.frame.FrameSlot; 9 | import com.oracle.truffle.api.frame.FrameUtil; 10 | import com.oracle.truffle.api.frame.VirtualFrame; 11 | import com.oracle.truffle.api.nodes.NodeInfo; 12 | 13 | @NodeInfo(shortName = "&") 14 | public class GoUnaryAddressNode extends GoUnaryNode { 15 | 16 | private FrameSlot value; 17 | private boolean isIndex; 18 | private GoExpressionNode index; 19 | 20 | public GoUnaryAddressNode(FrameSlot value, GoExpressionNode index){ 21 | this.value = value; 22 | this.isIndex = true; 23 | this.index = index; 24 | } 25 | 26 | public GoUnaryAddressNode(FrameSlot value){ 27 | this.value = value; 28 | this.isIndex = false; 29 | } 30 | 31 | @Override 32 | public String toString(){ 33 | return "& Node"; 34 | } 35 | 36 | @Override 37 | public Object executeGeneric(VirtualFrame frame) { 38 | if(isIndex){ 39 | GoArrayLikeTypes array = (GoArrayLikeTypes) FrameUtil.getObjectSafe(frame, value); 40 | Object arrayindex = index.executeGeneric(frame); 41 | return new GoArrayIndexPointerNode(array.hashCode(),array,arrayindex); 42 | } 43 | return GoPointerNode.createPointer(value, value.getKind()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /golang/Test.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | 4 | type Rectangle struct { 5 | width int 6 | height int 7 | } 8 | func (r Rectangle) area() int { 9 | return r.width * r.height 10 | } 11 | 12 | func sum(x int, y int) int { 13 | return x + y 14 | } 15 | func threeSum(x int, y int, z int) int { 16 | return x + y + z 17 | } 18 | 19 | func vals() (int, int) { 20 | return 4, 2 21 | } 22 | 23 | func fib(x int) int { 24 | if x == 0 { 25 | return 0 26 | } 27 | 28 | if x == 1 { 29 | return 1 30 | } 31 | return fib(x-1) + fib(x-2) 32 | } 33 | 34 | func main() { 35 | //Struct and Struct Methods 36 | r := Rectangle{width:4, height:6} 37 | fmt.Println(r.area()) 38 | 39 | //Multiple Returns 40 | a, b := vals() 41 | fmt.Println(a, b) 42 | 43 | //Recursion 44 | fmt.Println(fib(7)) 45 | 46 | //Arrays 47 | var arr [7]int 48 | fmt.Println(arr) 49 | arr[0] = 10 50 | arr[4] = 70 51 | fmt.Println("Length: ", len(arr)) 52 | x := [10]int{10,9,8,7,6,5,4,3,2,1} 53 | fmt.Println(x) 54 | 55 | //Slices 56 | s := make([]string, 3) 57 | s[0] = "a" 58 | s[1] = "b" 59 | s[2] = "c" 60 | s = append(s, "d") 61 | fmt.Println(s) 62 | x := s[1:3] 63 | fmt.Println(x) 64 | 65 | //Maps 66 | m := make(map[string]int) 67 | m["k1"] = 7 68 | m["k2"] = 13 69 | fmt.Println("map:", m) 70 | delete(m,"k1") 71 | fmt.Println(m) 72 | a := map[string]int{"foo": 1, "bar": 2} 73 | a["foo"] = 10 74 | a["bar"] = 3 75 | fmt.Println(a) 76 | 77 | //Functions 78 | a := sum(2,4) 79 | b := threeSum(2,4, a) 80 | fmt.Println(a) 81 | fmt.Println(b) 82 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRIdentNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | import com.oracle.app.parser.ir.GoTruffle; 6 | import com.oracle.truffle.api.source.Source; 7 | import com.oracle.truffle.api.source.SourceSection; 8 | 9 | public class GoIRIdentNode extends GoBaseIRNode { 10 | 11 | String ident; 12 | GoBaseIRNode child; 13 | String namepos; 14 | // var x,y = multipleReturn() 15 | //assign pos tells which position it is in the above 16 | private int assignPos; 17 | 18 | public GoIRIdentNode(String ident, GoBaseIRNode child, String namepos) { 19 | super("Ident"); 20 | this.ident = ident; 21 | this.child = child; 22 | this.namepos = namepos; 23 | } 24 | 25 | //Will need to merge getIdent and getIdentifier so that only getIdentifier is used 26 | @Override 27 | public String getIdentifier(){ 28 | return ident; 29 | } 30 | 31 | public GoBaseIRNode getChild() { 32 | return child; 33 | } 34 | 35 | public void setPos(int a) { 36 | assignPos = a; 37 | } 38 | 39 | public int getAssignPos() { 40 | return assignPos; 41 | } 42 | 43 | @Override 44 | public Object accept(GoIRVisitor visitor) { 45 | return visitor.visitIdent(this); 46 | } 47 | 48 | public SourceSection getSource(Source source) { 49 | String[] split = namepos.split(":"); 50 | int linenum = Integer.parseInt(split[1]); 51 | int charindex = Integer.parseInt(split[2]); 52 | return source.createSection(linenum,charindex,ident.length()); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/StructTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.oracle.truffle.api.source.Source; 10 | import com.oracle.truffle.api.vm.PolyglotEngine; 11 | 12 | public class StructTest { 13 | 14 | private PolyglotEngine engine; 15 | private PolyglotEngine.Value math; 16 | 17 | @Before 18 | public void setUp() throws Exception { 19 | engine = PolyglotEngine.newBuilder().build(); 20 | Source source = Source.newBuilder("" 21 | +"package main\n" 22 | +"\n" 23 | +"type Vertex struct {\n" 24 | +" X int\n" 25 | +" Y int\n" 26 | +"}\n" 27 | +"\n" 28 | +"func main() int {\n" 29 | +" v := Vertex{X:1, Y:2}\n" 30 | +" v.X = 4\n" 31 | +" return v.X\n" 32 | +"}\n" 33 | ). 34 | name("UnitTest.go"). 35 | mimeType("text/x-go"). 36 | build(); 37 | ToolChain.executeCommands(source); 38 | engine.eval(source); 39 | math = engine.findGlobalSymbol("main"); 40 | } 41 | 42 | @After 43 | public void tearDown() throws Exception { 44 | engine.dispose(); 45 | } 46 | 47 | @Test 48 | public void test() throws Exception{ 49 | 50 | Number ret = math.execute().as(Number.class); 51 | assertEquals(4,ret.intValue()); 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoReturnNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoArrayExprNode; 4 | import com.oracle.app.nodes.GoExpressionNode; 5 | import com.oracle.app.nodes.GoStatementNode; 6 | import com.oracle.app.runtime.GoNull; 7 | import com.oracle.truffle.api.frame.VirtualFrame; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | 10 | @NodeInfo(shortName = "return", description = "The node implementing a return statement") 11 | public final class GoReturnNode extends GoStatementNode { 12 | 13 | @Child private GoExpressionNode valueNode; 14 | public int paramNum = 0; 15 | public GoReturnNode(GoExpressionNode valueNode) { 16 | this.valueNode = valueNode; 17 | } 18 | 19 | @Override 20 | public void executeVoid(VirtualFrame frame) { 21 | Object result; 22 | if (valueNode != null) { 23 | if( ((GoArrayExprNode) valueNode).getArguments().length ==1) { 24 | result = ((GoArrayExprNode) valueNode).getArguments()[0].executeGeneric(frame); 25 | } 26 | else { 27 | result = ((GoArrayExprNode) valueNode).getArguments()[paramNum].executeGeneric(frame); 28 | paramNum++; 29 | if(paramNum >=((GoArrayExprNode) valueNode).getArguments().length ) { 30 | paramNum = 0; 31 | } 32 | } 33 | } else { 34 | /* 35 | * Return statement that was not followed by an expression, so return the null value. 36 | */ 37 | result = GoNull.SINGLETON; 38 | } 39 | //this throw is what makes it actualy return 40 | throw new GoReturnException(result); 41 | } 42 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/local/GoWriteMemoryNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.local; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.types.GoPointerNode.GoArrayIndexPointerNode; 5 | import com.oracle.app.nodes.types.GoPointerNode.GoIntPointerNode; 6 | import com.oracle.app.nodes.types.GoPointerNode.GoObjectPointerNode; 7 | import com.oracle.truffle.api.dsl.NodeChild; 8 | import com.oracle.truffle.api.dsl.Specialization; 9 | import com.oracle.truffle.api.frame.FrameSlot; 10 | import com.oracle.truffle.api.frame.VirtualFrame; 11 | 12 | /** 13 | * Handles all pointer writes and mimics a memory write by writing directly to the referenced {@link FrameSlot} 14 | * Is essentially structured the same as {@link GoReadLocalVariableNode} except 15 | * it needs to extract the pointer object first, then set the value in the slot the pointer points at. 16 | * @author Trevor 17 | * 18 | */ 19 | @NodeChild("valueNode") 20 | @NodeChild(value = "pointee", type = GoReadLocalVariableNode.class) 21 | public abstract class GoWriteMemoryNode extends GoExpressionNode { 22 | 23 | @Specialization 24 | protected int writeInt(VirtualFrame frame, int value, GoIntPointerNode ptr){ 25 | frame.setInt(ptr.getSlot(), value); 26 | return value; 27 | } 28 | 29 | @Specialization 30 | protected Object writeArrayIndex(VirtualFrame frame, Object value, GoArrayIndexPointerNode ptr){ 31 | ptr.insert(value); 32 | return null; 33 | } 34 | 35 | @Specialization 36 | protected Object write(VirtualFrame frame, Object value, GoObjectPointerNode ptr){ 37 | frame.setObject(ptr.getSlot(), value); 38 | return value; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRInvokeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.app.parser.ir.GoIRVisitor; 5 | 6 | public class GoIRInvokeNode extends GoBaseIRNode { 7 | 8 | GoBaseIRNode functionNode; 9 | GoIRArrayListExprNode argumentNodes; 10 | String lparen; 11 | String ellipsis; 12 | String rparen; 13 | int assignLen = 0; 14 | 15 | public GoIRInvokeNode(GoBaseIRNode functionNode, GoIRArrayListExprNode argumentNodes,String lparen,String ellipsis,String rparen) { 16 | super("Call Expr (Invoke)"); 17 | this.functionNode = functionNode; 18 | this.argumentNodes = argumentNodes; 19 | this.lparen = lparen; 20 | this.ellipsis = ellipsis; 21 | this.rparen = rparen; 22 | } 23 | 24 | public GoBaseIRNode getFunctionNode() { 25 | return functionNode; 26 | } 27 | 28 | public int getEndPos(){ 29 | int endpos = Integer.parseInt(rparen.split(":")[2]); 30 | return endpos; 31 | } 32 | 33 | public void incAssignLen() { 34 | assignLen++; 35 | } 36 | 37 | public int getAssignLen() { 38 | return assignLen; 39 | } 40 | 41 | public int getPositionOfRightParen(){ 42 | String[] split = rparen.split(":"); 43 | return Integer.parseInt(split[2]); 44 | } 45 | 46 | /* 47 | * Maybe not needed 48 | */ 49 | public GoIRArrayListExprNode getArgumentNode(){ 50 | return argumentNodes; 51 | } 52 | 53 | public int getArgumentsSize(){ 54 | return argumentNodes.getSize(); 55 | } 56 | 57 | @Override 58 | public Object accept(GoIRVisitor visitor) { 59 | return visitor.visitInvoke(this); 60 | } 61 | 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/RecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.oracle.truffle.api.source.Source; 10 | import com.oracle.truffle.api.vm.PolyglotEngine; 11 | 12 | public class RecursionTest { 13 | 14 | private PolyglotEngine engine; 15 | private PolyglotEngine.Value math; 16 | 17 | @Before 18 | public void setUp() throws Exception { 19 | engine = PolyglotEngine.newBuilder().build(); 20 | Source source = Source.newBuilder("" 21 | +"package main\n" 22 | +"\n" 23 | +"func fib(x int) int {\n" 24 | +" if x <= 1 {\n" 25 | +" return 1\n" 26 | +" } else {\n" 27 | +" return fib(x - 1) + fib(x - 2)\n" 28 | +" }\n" 29 | +"}\n" 30 | +"func main() int{\n" 31 | +" a := fib(5)\n" 32 | +" return a \n" 33 | +"}"). 34 | name("UnitTest.go"). 35 | mimeType("text/x-go"). 36 | build(); 37 | ToolChain.executeCommands(source); 38 | engine.eval(source); 39 | math = engine.findGlobalSymbol("main"); 40 | } 41 | 42 | @After 43 | public void tearDown() throws Exception { 44 | engine.dispose(); 45 | } 46 | 47 | @Test 48 | public void test() throws Exception{ 49 | 50 | Number ret = math.execute().as(Number.class); 51 | assertEquals(8,ret.intValue()); 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRSliceExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import java.util.ArrayList; 4 | 5 | import com.oracle.app.parser.ir.GoBaseIRNode; 6 | import com.oracle.app.parser.ir.GoIRVisitor; 7 | 8 | public class GoIRSliceExprNode extends GoBaseIRNode { 9 | 10 | GoBaseIRNode expr; 11 | String lbrack; 12 | GoBaseIRNode low; 13 | GoBaseIRNode high; 14 | GoBaseIRNode max; 15 | boolean slice3; 16 | String rbrack; 17 | 18 | public GoIRSliceExprNode(GoBaseIRNode sliceexpr, String slbrack, GoBaseIRNode low, GoBaseIRNode high, 19 | GoBaseIRNode max, String slice3, String srbrack) { 20 | super("Slice Expr Node"); 21 | lbrack = slbrack; 22 | rbrack = srbrack; 23 | this.slice3 = Boolean.getBoolean(slice3); 24 | expr = sliceexpr; 25 | this.low = low; 26 | this.high = high; 27 | this.max = max; 28 | } 29 | 30 | public GoBaseIRNode getExpr() { 31 | return expr; 32 | } 33 | 34 | public int getRbrackLineNum() { 35 | String[] split = rbrack.split(":"); 36 | return Integer.parseInt(split[1]); 37 | } 38 | 39 | public int getRbrackStartColumn(){ 40 | String[] split = rbrack.split(":"); 41 | return Integer.parseInt(split[2]); 42 | } 43 | 44 | public GoBaseIRNode getLow() { 45 | return low; 46 | } 47 | 48 | public GoBaseIRNode getHigh() { 49 | return high; 50 | } 51 | 52 | public GoBaseIRNode getMax() { 53 | return max; 54 | } 55 | 56 | public boolean isSlice3() { 57 | return slice3; 58 | } 59 | 60 | public String getRbrack() { 61 | return rbrack; 62 | } 63 | 64 | @Override 65 | public Object accept(GoIRVisitor visitor) { 66 | return visitor.visitSliceExpr(this); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoFunctionBodyNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoStatementNode; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | import com.oracle.truffle.api.nodes.NodeInfo; 7 | import com.oracle.truffle.api.profiles.BranchProfile; 8 | 9 | @NodeInfo(shortName = "body") 10 | public final class GoFunctionBodyNode extends GoExpressionNode { 11 | 12 | /** The body of the function. */ 13 | @Child private GoStatementNode bodyNode; 14 | 15 | private final BranchProfile exceptionTaken = BranchProfile.create(); 16 | private final BranchProfile nullTaken = BranchProfile.create(); 17 | 18 | public GoFunctionBodyNode(GoStatementNode bodyNode) { 19 | this.bodyNode = bodyNode; 20 | addRootTag(); 21 | } 22 | 23 | @Override 24 | public Object executeGeneric(VirtualFrame frame) { 25 | try { 26 | /* Execute the function body. */ 27 | bodyNode.executeVoid(frame); 28 | 29 | } catch (GoReturnException ex) { 30 | /* 31 | * In the interpreter, record profiling information that the function has an explicit 32 | * return. 33 | */ 34 | exceptionTaken.enter(); 35 | /* The exception transports the actual return value. */ 36 | return ex.getResult(); 37 | } 38 | 39 | /* 40 | * In the interpreter, record profiling information that the function ends without an 41 | * explicit return. 42 | */ 43 | nullTaken.enter(); 44 | /* Return the default null value. */ 45 | return null; 46 | } 47 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/runtime/GoFunction.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.runtime; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.nodes.GoUndefinedFunctionRootNode; 5 | import com.oracle.truffle.api.Assumption; 6 | import com.oracle.truffle.api.RootCallTarget; 7 | import com.oracle.truffle.api.Truffle; 8 | import com.oracle.truffle.api.interop.ForeignAccess; 9 | import com.oracle.truffle.api.interop.TruffleObject; 10 | import com.oracle.truffle.api.utilities.CyclicAssumption; 11 | 12 | 13 | public final class GoFunction implements TruffleObject { 14 | 15 | /** The name of the function. */ 16 | private final String name; 17 | 18 | /** The current implementation of this function. */ 19 | private RootCallTarget callTarget; 20 | 21 | private final CyclicAssumption callTargetStable; 22 | 23 | public GoFunction(GoLanguage language, String name) { 24 | this.name = name; 25 | this.callTarget = Truffle.getRuntime().createCallTarget(new GoUndefinedFunctionRootNode(language, name)); 26 | this.callTargetStable = new CyclicAssumption(name); 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setCallTarget(RootCallTarget callTarget) { 34 | this.callTarget = callTarget; 35 | 36 | } 37 | 38 | public RootCallTarget getCallTarget() { 39 | return callTarget; 40 | } 41 | 42 | public Assumption getCallTargetStable() { 43 | return callTargetStable.getAssumption(); 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return name; 49 | } 50 | 51 | @Override 52 | public ForeignAccess getForeignAccess() { 53 | return GoFunctionMessageResolutionForeign.ACCESS; 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/ControlFlowTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.io.IOException; 6 | 7 | import org.junit.After; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | 11 | import com.oracle.truffle.api.source.Source; 12 | import com.oracle.truffle.api.vm.PolyglotEngine; 13 | 14 | public class ControlFlowTest { 15 | 16 | private PolyglotEngine engine; 17 | private PolyglotEngine.Value math; 18 | 19 | @Before 20 | public void setUp() throws IOException, InterruptedException{ 21 | engine = PolyglotEngine.newBuilder().build(); 22 | Source source = Source.newBuilder("" 23 | + "package main \n" 24 | + "\n" 25 | + "func main() int{ \n" 26 | + " a := 1 \n" 27 | + " if a == 2 { \n" 28 | + " a = 2 \n" 29 | + " } else if num := 10; num < 0 { \n" 30 | + " a = 3 \n" 31 | + " } else { \n" 32 | + " a = 4 \n" 33 | + " } \n" 34 | + "\n" 35 | + " i := 3 \n" 36 | + " for i <= 5 { \n" 37 | + " a += 1 \n" 38 | + " i += 1 \n" 39 | + " } \n" 40 | + "\n" 41 | + " switch a { \n" 42 | + " case 7: \n" 43 | + " a = 100 \n" 44 | + " case 5: \n" 45 | + " a = 50 \n" 46 | + " default: \n" 47 | + " a = 10 \n" 48 | + " } \n" 49 | + " return a \n" 50 | + "}"). 51 | name("UnitTest.go"). 52 | mimeType("text/x-go").build(); 53 | ToolChain.executeCommands(source); 54 | engine.eval(source); 55 | math = engine.findGlobalSymbol("main"); 56 | } 57 | 58 | @After 59 | public void tearDown(){ 60 | engine.dispose(); 61 | } 62 | 63 | @Test 64 | public void test(){ 65 | Number ret = math.execute().as(Number.class); 66 | assertEquals(100,ret.intValue()); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /golang/src/test/java/com/oracle/app/SliceTest.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Test; 8 | 9 | import com.oracle.truffle.api.source.Source; 10 | import com.oracle.truffle.api.vm.PolyglotEngine; 11 | 12 | public class SliceTest { 13 | private PolyglotEngine engine; 14 | private PolyglotEngine.Value math; 15 | 16 | @Before 17 | public void setUp() throws Exception { 18 | engine = PolyglotEngine.newBuilder().build(); 19 | Source source = Source.newBuilder("" 20 | +"package main\n" 21 | +"\n" 22 | +"func main() int{ \n" 23 | + " result := 0 \n" 24 | + " b := [5]int{1 , 2, 3, 4, 5} \n" 25 | + " s := b[1:3] \n" 26 | + " for j := 0; j < len(s); j++ { \n" 27 | + " result += s[j] \n" 28 | + " } \n" 29 | + " s = b[:3] \n" 30 | + " for j := 0; j < len(s); j++ { \n" 31 | + " result += s[j] \n" 32 | + " } \n" 33 | + " s = b[3:] \n" 34 | + " for j := 0; j < len(s); j++ { \n" 35 | + " result += s[j] \n" 36 | + " } \n" 37 | + " s[0] = 10 \n" 38 | + " for j := 0; j < len(s); j++ { \n" 39 | + " result += s[j] \n" 40 | + " } \n" 41 | + " return result \n" 42 | +"}"). 43 | name("UnitTest.go"). 44 | mimeType("text/x-go"). 45 | build(); 46 | ToolChain.executeCommands(source); 47 | engine.eval(source); 48 | math = engine.findGlobalSymbol("main"); 49 | } 50 | 51 | @After 52 | public void tearDown() throws Exception { 53 | engine.dispose(); 54 | } 55 | 56 | @Test 57 | public void test() throws Exception{ 58 | 59 | Number ret = math.execute().as(Number.class); 60 | assertEquals(35,ret.intValue()); 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoForBreakNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoStatementNode; 4 | import com.oracle.truffle.api.frame.VirtualFrame; 5 | import com.oracle.truffle.api.nodes.Node; 6 | import com.oracle.truffle.api.nodes.RepeatingNode; 7 | import com.oracle.truffle.api.profiles.BranchProfile; 8 | import com.oracle.truffle.api.source.SourceSection; 9 | 10 | public class GoForBreakNode extends Node implements RepeatingNode { 11 | 12 | @Child private GoStatementNode bodyNode; 13 | 14 | 15 | private final BranchProfile continueTaken = BranchProfile.create(); 16 | private final BranchProfile breakTaken = BranchProfile.create(); 17 | 18 | private SourceSection sourceSection; 19 | 20 | public GoForBreakNode(GoStatementNode bodyNode) { 21 | this.bodyNode = bodyNode; 22 | } 23 | 24 | @Override 25 | public SourceSection getSourceSection() { 26 | return sourceSection; 27 | } 28 | 29 | public void setSourceSection(SourceSection section) { 30 | assert this.sourceSection == null : "overwriting existing SourceSection"; 31 | this.sourceSection = section; 32 | } 33 | 34 | @Override 35 | public boolean executeRepeating(VirtualFrame frame) { 36 | 37 | try { 38 | bodyNode.executeVoid(frame); 39 | return true; 40 | 41 | } catch (GoContinueException ex) { 42 | continueTaken.enter(); 43 | 44 | return true; 45 | 46 | } catch (GoBreakException ex) { 47 | breakTaken.enter(); 48 | 49 | return false; 50 | } 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return GoStatementNode.formatSourceSection(this); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/local/GoWriteProperty.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.local; 2 | 3 | import com.oracle.app.GoException; 4 | import com.oracle.truffle.api.dsl.Cached; 5 | import com.oracle.truffle.api.dsl.Specialization; 6 | import com.oracle.truffle.api.nodes.Node; 7 | import com.oracle.truffle.api.object.DynamicObject; 8 | import com.oracle.truffle.api.object.Property; 9 | import com.oracle.truffle.api.object.Shape; 10 | import com.oracle.truffle.api.object.Shape.Allocator; 11 | 12 | public abstract class GoWriteProperty extends Node { 13 | 14 | protected boolean createProperty; 15 | 16 | public GoWriteProperty(boolean createProperty){ 17 | this.createProperty = createProperty; 18 | } 19 | 20 | public abstract Object executeWrite(Object receiver, Object name, Object value); 21 | 22 | @Specialization(guards = "isPropertyWrite()") 23 | public static Object writeExistingProperty(DynamicObject receiver, Object name, Object value){ 24 | if(!receiver.set(name, value)){ 25 | throw new GoException("undefined type, no field or method "+name); 26 | } 27 | return receiver; 28 | } 29 | 30 | //Property writes only occur for Struct function writes currently. 31 | //TODO Possibly set a flag when defining a property. And maybe use cache to retrieve the shape and location 32 | @Specialization 33 | public static Object writeNewProperty(Shape oldShape, Object name, Object value, 34 | @Cached("getAllocator(oldShape)") Allocator allocator){ 35 | Property property = Property.create(name,allocator.declaredLocation(value),1); 36 | Shape newShape = oldShape.addProperty(property); 37 | return newShape; 38 | } 39 | 40 | protected Allocator getAllocator(Shape shape){ 41 | return shape.allocator(); 42 | } 43 | 44 | protected boolean isPropertyWrite(){ 45 | return !createProperty; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoDivNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import java.math.BigInteger; 4 | 5 | import com.oracle.app.nodes.GoBinaryNode; 6 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 7 | import com.oracle.truffle.api.dsl.Specialization; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | 10 | //Should throw a runtime GoException when dividing by 0 11 | /* 12 | * package main 13 | import "fmt" 14 | 15 | func main() { 16 | x := 0 17 | fmt.Println(3/x) 18 | } 19 | */ 20 | @NodeInfo(shortName = "/") 21 | public abstract class GoDivNode extends GoBinaryNode { 22 | 23 | @Specialization(rewriteOn = ArithmeticException.class) 24 | protected int div(int left, int right) throws ArithmeticException { 25 | int result = left / right; 26 | 27 | if ((left & right & result) < 0) { 28 | throw new ArithmeticException("integer overflow"); 29 | } 30 | return result; 31 | } 32 | 33 | @Specialization(rewriteOn = ArithmeticException.class) 34 | protected float div(float left, float right) throws ArithmeticException { 35 | float result = left / right; 36 | 37 | /*//TODO 38 | if ((left & right & result) < 0) { 39 | throw new ArithmeticException("float overflow"); 40 | }*/ 41 | return result; 42 | } 43 | 44 | @Specialization(rewriteOn = ArithmeticException.class) 45 | protected double div(double left, double right) throws ArithmeticException { 46 | double result = left / right; 47 | 48 | /*//TODO 49 | if ((left & right & result) < 0) { 50 | throw new ArithmeticException("float overflow"); 51 | }*/ 52 | return result; 53 | } 54 | 55 | @Specialization 56 | @TruffleBoundary 57 | protected BigInteger div(BigInteger left, BigInteger right) { 58 | return left.divide(right); 59 | } 60 | } -------------------------------------------------------------------------------- /golang/gt: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | JAVACMD=${JAVACMD:=./graalvm/bin/java} 4 | 5 | PROGRAM_ARGS="" 6 | JAVA_ARGS="" 7 | verbose=0 8 | 9 | for opt in "$@" 10 | do 11 | case $opt in 12 | -debug) 13 | JAVA_ARGS="$JAVA_ARGS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=y" ;; 14 | -dump) 15 | JAVA_ARGS="$JAVA_ARGS -Dgraal.Dump= -Dgraal.MethodFilter=Truffle.* -Dgraal.TruffleBackgroundCompilation=false -Dgraal.TraceTruffleCompilation=true -Dgraal.TraceTruffleCompilationDetails=true" ;; 16 | -disassemble) 17 | JAVA_ARGS="$JAVA_ARGS -XX:CompileCommand=print,*OptimizedCallTarget.callRoot -XX:CompileCommand=exclude,*OptimizedCallTarget.callRoot -Dgraal.TruffleBackgroundCompilation=false -Dgraal.TraceTruffleCompilation=true -Dgraal.TraceTruffleCompilationDetails=true" ;; 18 | -J*) 19 | opt=${opt:2} 20 | JAVA_ARGS="$JAVA_ARGS $opt" ;; 21 | -go) 22 | verbose=1 ;; 23 | *) 24 | PROGRAM_ARGS="$PROGRAM_ARGS $opt" ;; 25 | esac 26 | done 27 | 28 | TRUFFLE_LIB=./graalvm/jre/lib/truffle 29 | 30 | if [ $verbose -eq 1 ]; then 31 | TruffleOutput="${PROGRAM_ARGS}.Truffle.output" 32 | GoOutput="${PROGRAM_ARGS}.Go.output" 33 | DIRO="TestOutputs/" 34 | echo "Truffle Output:" 35 | $JAVACMD $JAVA_ARGS -XX:-UseJVMCIClassLoader -Xbootclasspath/a:$TRUFFLE_LIB/truffle-api.jar:$TRUFFLE_LIB/locator.jar:$TRUFFLE_LIB/truffle-nfi.jar -cp ./target/classes com.oracle.app.GoMain $PROGRAM_ARGS | tee "$DIRO""${TruffleOutput}" 36 | 37 | go run $PROGRAM_ARGS &>"$DIRO""${GoOutput}" 38 | echo "Go Output:" 39 | cat "$DIRO""${GoOutput}" 40 | echo "Any diff listed below:" 41 | diff "$DIRO""${GoOutput}" "$DIRO""${TruffleOutput}" 42 | rm -f *.ast 43 | else 44 | $JAVACMD $JAVA_ARGS -XX:-UseJVMCIClassLoader -Xbootclasspath/a:$TRUFFLE_LIB/truffle-api.jar:$TRUFFLE_LIB/locator.jar:$TRUFFLE_LIB/truffle-nfi.jar -cp ./target/classes com.oracle.app.GoMain $PROGRAM_ARGS 45 | fi 46 | #rm -f *.ast 47 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/GoEvalRootNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.runtime.GoContext; 5 | import com.oracle.app.runtime.GoNull; 6 | import com.oracle.truffle.api.frame.FrameDescriptor; 7 | import com.oracle.truffle.api.frame.VirtualFrame; 8 | import com.oracle.truffle.api.source.SourceSection; 9 | 10 | /** 11 | * In addition to {@link GoRootNode}, this class performs two additional tasks: 12 | * 13 | *
    14 | *
  • Lazily registration of functions on first execution. This fulfills the semantics of 15 | * "evaluating" source code in Go.
  • 16 | *
  • Conversion of arguments to types understood by Go. The Go source code can be evaluated from a 17 | * different language, i.e., the caller can be a node from a different language that uses types not 18 | * understood by Go.
  • 19 | *
20 | */ 21 | public final class GoEvalRootNode extends GoRootNode { 22 | 23 | public GoEvalRootNode(GoLanguage language, FrameDescriptor frameDescriptor, GoExpressionNode bodyNode, SourceSection sourceSection, String name) { 24 | super(language, frameDescriptor, null, bodyNode, sourceSection, name); 25 | } 26 | 27 | @Override 28 | public Object execute(VirtualFrame frame) { 29 | 30 | if (getBodyNode() == null) { 31 | /* The source code did not have a "main" function, so nothing to execute. */ 32 | System.out.println("FAILURE NO BODYNODE"); 33 | return GoNull.SINGLETON; 34 | } 35 | 36 | /* Conversion of arguments to types understood by Go. */ 37 | Object[] arguments = frame.getArguments(); 38 | for (int i = 0; i < arguments.length; i++) { 39 | arguments[i] = GoContext.fromForeignValue(arguments[i]); 40 | } 41 | 42 | /* Now we can execute the body of the "main" function. */ 43 | return super.execute(frame); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoStructTypeExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.GoLanguage; 4 | import com.oracle.app.nodes.GoExpressionNode; 5 | import com.oracle.app.nodes.call.GoFieldNode; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | import com.oracle.truffle.api.object.DynamicObject; 8 | import com.oracle.truffle.api.object.DynamicObjectFactory; 9 | import com.oracle.truffle.api.object.Shape; 10 | 11 | /** 12 | * Creates the struct dynamic object and defines its fields. Returns the shape of the struct. 13 | * Currently fields can only be created if the field is on its own line. 14 | * TODO Change FieldNodes, possibly make struct a Shape rather then a DynamicObject 15 | * @author Trevor 16 | * 17 | */ 18 | public class GoStructTypeExprNode extends GoExpressionNode{ 19 | GoFieldNode[] fields; 20 | DynamicObject struct = getNewStruct(); 21 | 22 | public GoStructTypeExprNode(GoFieldNode[] fields){ 23 | this.fields = fields; 24 | } 25 | 26 | public Object executeGeneric(VirtualFrame frame){ 27 | 28 | for(GoFieldNode child : fields){ 29 | Object type = child.getType().executeGeneric(frame); 30 | String name = child.getName(); // This is the name of the field 31 | struct.define(name, type); 32 | /* 33 | FieldNode field = new FieldNode(type.executeGeneric(frame), (String) type.getSlot().getIdentifier()); 34 | 35 | //If name is null (check fieldNode handler function in GoTruffle) 36 | if(name != null){ 37 | result.insertField(name.getName(),field); 38 | } else { 39 | result.insertField((String) type.getSlot().getIdentifier(), field); 40 | } 41 | */ 42 | } 43 | return struct.getShape(); 44 | } 45 | 46 | public DynamicObject getNewStruct(){ 47 | return GoLanguage.getCurrentContext().createStruct(); 48 | } 49 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/runtime/GoFunctionMessageResolution.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.runtime; 2 | 3 | import static com.oracle.app.runtime.GoContext.fromForeignValue; 4 | 5 | import com.oracle.app.nodes.call.GoGenericDispatchNode; 6 | import com.oracle.app.nodes.call.GoGenericDispatchNodeGen; 7 | import com.oracle.app.nodes.interop.GoTypeToForeignNode; 8 | import com.oracle.app.nodes.interop.GoTypeToForeignNodeGen; 9 | import com.oracle.truffle.api.interop.CanResolve; 10 | import com.oracle.truffle.api.interop.MessageResolution; 11 | import com.oracle.truffle.api.interop.Resolve; 12 | import com.oracle.truffle.api.interop.TruffleObject; 13 | import com.oracle.truffle.api.nodes.Node; 14 | 15 | @MessageResolution(receiverType = GoFunction.class) 16 | public class GoFunctionMessageResolution { 17 | 18 | @Resolve(message = "EXECUTE") 19 | public abstract static class GoForeignFunctionExecuteNode extends Node{ 20 | 21 | @Child private GoGenericDispatchNode dispatch = GoGenericDispatchNodeGen.create(); 22 | @Child private GoTypeToForeignNode toForeign = GoTypeToForeignNodeGen.create(); 23 | 24 | public Object access(GoFunction receiver, Object[] arguments){ 25 | Object[] arr = new Object[arguments.length]; 26 | 27 | for(int i = 0; i < arr.length; i++){ 28 | arr[i] = fromForeignValue(arguments[i]); 29 | } 30 | Object result = dispatch.executeDispatch(receiver, arr); 31 | if(result == null){ 32 | result = GoNull.SINGLETON; 33 | } 34 | return toForeign.executeConvert(result); 35 | } 36 | } 37 | 38 | @Resolve(message = "IS_EXECUTABLE") 39 | public abstract static class GoForeignIsExecutableNode extends Node { 40 | public Object access(Object receiver) { 41 | return receiver instanceof GoFunction; 42 | } 43 | } 44 | 45 | @CanResolve 46 | public abstract static class CheckFunction extends Node{ 47 | protected static boolean test(TruffleObject receiver){ 48 | return receiver instanceof GoFunction; 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/GoMain.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.PrintStream; 8 | 9 | import com.oracle.app.runtime.GoNull; 10 | import com.oracle.truffle.api.Truffle; 11 | import com.oracle.truffle.api.source.Source; 12 | import com.oracle.truffle.api.vm.PolyglotEngine; 13 | import com.oracle.truffle.api.vm.PolyglotEngine.Value; 14 | 15 | public class GoMain 16 | { 17 | public static void main(String[] args) throws IOException, InterruptedException { 18 | 19 | Source source; 20 | if(args.length == 0){ 21 | System.out.println("Don't know about standard input quite yet"); 22 | source = Source.newBuilder(new InputStreamReader(System.in)). 23 | name(""). 24 | mimeType(GoLanguage.MIME_TYPE). 25 | build(); 26 | } 27 | else{ 28 | 29 | String goFile = args[0]; 30 | ToolChain.executeCommands(goFile); 31 | source = Source.newBuilder(new File(goFile)).build(); 32 | } 33 | executeSource(source, System.in, System.out); 34 | 35 | } 36 | 37 | private static void executeSource(Source source, InputStream in, PrintStream out){ 38 | PolyglotEngine engine = PolyglotEngine.newBuilder().setIn(in).setOut(out).build(); 39 | assert engine.getLanguages().containsKey(GoLanguage.MIME_TYPE); 40 | try { 41 | Value result = engine.eval(source); 42 | 43 | if(result == null) { 44 | //Throw error GoException 45 | } 46 | else if (result.get() != GoNull.SINGLETON ) { 47 | //This thing here is actually the reason why we get that last null LOL 48 | //out.println(result.get()); 49 | } 50 | } 51 | //All errors end up here 52 | catch(GoException e){ 53 | 54 | } 55 | catch (Throwable ex){ 56 | 57 | ex.printStackTrace(out); 58 | } 59 | 60 | engine.dispose(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/ToolChain.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.OutputStreamWriter; 7 | import java.io.PrintWriter; 8 | import java.io.Writer; 9 | 10 | import com.oracle.truffle.api.source.Source; 11 | 12 | /** 13 | * Runs the GoLang compiler frontend and spits out the ast tree for use on the backend 14 | * 15 | */ 16 | public class ToolChain { 17 | 18 | public static void executeCommands(Source goFile) throws IOException, InterruptedException { 19 | PrintWriter writer = new PrintWriter(goFile.getName(), "UTF-8"); 20 | writer.print(goFile.getCharacters()); 21 | writer.close(); 22 | executeCommands(goFile.getName()); 23 | } 24 | 25 | public static void executeCommands(String goFile) throws IOException, InterruptedException { 26 | 27 | String astFile = goFile.substring(0, goFile.lastIndexOf('.')) + ".ast"; 28 | File tempScript = createTempScript(goFile, astFile); 29 | 30 | try { 31 | ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString()); 32 | pb.inheritIO(); 33 | Process process = pb.start(); 34 | process.waitFor(); 35 | } finally { 36 | tempScript.delete(); 37 | } 38 | 39 | } 40 | 41 | public static File createTempScript(String goFile, String astFile) throws IOException { 42 | 43 | File tempScript = File.createTempFile("script", null); 44 | 45 | Writer streamWriter = new OutputStreamWriter(new FileOutputStream(tempScript)); 46 | PrintWriter printWriter = new PrintWriter(streamWriter); 47 | //Assumed to have the printast file 48 | printWriter.println("#!/bin/bash"); 49 | //System.out.println("go build " + "printAST.go"); 50 | printWriter.println("go build " + "printast.go"); 51 | //System.out.println("./printast "+ goFile + " > " + astFile); 52 | printWriter.println("./printast "+ goFile + " > " + astFile); 53 | //printWriter.println("rm printast"); 54 | 55 | printWriter.close(); 56 | 57 | return tempScript; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /docs/css/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | overflow-x: hidden; 3 | padding-bottom: 20px; 4 | } 5 | 6 | #wrapper { 7 | padding-left: 0; 8 | -webkit-transition: all 0.5s ease; 9 | -moz-transition: all 0.5s ease; 10 | -o-transition: all 0.5s ease; 11 | transition: all 0.5s ease; 12 | } 13 | 14 | #wrapper.toggled { 15 | padding-left: 250px; 16 | } 17 | 18 | #sidebar-wrapper { 19 | z-index: 1000; 20 | position: fixed; 21 | left: 250px; 22 | width: 0; 23 | height: 100%; 24 | margin-left: -250px; 25 | overflow-y: auto; 26 | background: #000; 27 | -webkit-transition: all 0.5s ease; 28 | -moz-transition: all 0.5s ease; 29 | -o-transition: all 0.5s ease; 30 | transition: all 0.5s ease; 31 | } 32 | 33 | #wrapper.toggled #sidebar-wrapper { 34 | width: 250px; 35 | } 36 | 37 | .sidebar-nav { 38 | position: absolute; 39 | top: 0; 40 | width: 250px; 41 | margin: 0; 42 | padding: 0; 43 | list-style: none; 44 | } 45 | 46 | .sidebar-nav li { 47 | text-indent: 20px; 48 | line-height: 35px; 49 | } 50 | 51 | .sidebar-nav li a { 52 | display: block; 53 | text-decoration: none; 54 | color: #999999; 55 | } 56 | 57 | .sidebar-nav li a:hover { 58 | text-decoration: none; 59 | color: #fff; 60 | background: rgba(255, 255, 255, 0.2); 61 | } 62 | 63 | .sidebar-nav li a:active, .sidebar-nav li a:focus { 64 | text-decoration: none; 65 | } 66 | 67 | .sidebar-nav>.sidebar-brand { 68 | height: 65px; 69 | font-size: 18px; 70 | line-height: 60px; 71 | } 72 | 73 | .sidebar-nav>.sidebar-brand a { 74 | color: #999999; 75 | } 76 | 77 | .sidebar-nav>.sidebar-brand a:hover { 78 | color: #fff; 79 | background: none; 80 | } 81 | 82 | .jumbotron { 83 | font-size: 4em; 84 | background-color:#bbb8b8; 85 | } 86 | 87 | p span { 88 | display: block; 89 | } 90 | 91 | .indent { 92 | text-indent: 16px; 93 | } 94 | 95 | .dblIndent { 96 | text-indent: 32px; 97 | } 98 | 99 | .box { 100 | background-color: #dcdbdb; 101 | border-radius: 6px; 102 | padding-right: 5px; 103 | } 104 | 105 | div.box p { 106 | padding: 10px; 107 | } 108 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/fmt/FmtFunctionList.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins.fmt; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import com.oracle.app.GoException; 7 | import com.oracle.app.GoLanguage; 8 | import com.oracle.app.nodes.GoExpressionNode; 9 | import com.oracle.app.nodes.GoRootNode; 10 | import com.oracle.app.runtime.GoContext; 11 | import com.oracle.app.runtime.GoFunction; 12 | import com.oracle.truffle.api.RootCallTarget; 13 | import com.oracle.truffle.api.Truffle; 14 | import com.oracle.truffle.api.frame.FrameDescriptor; 15 | import com.oracle.truffle.api.frame.VirtualFrame; 16 | 17 | /** 18 | * Imports keep track of their own function definitions. 19 | * 1) Creating new imports will require a parent class to contain all imports 20 | * 2) A way to parse imports or simulate parsing library imports so that they return a function registry 21 | * @author Trevor 22 | * 23 | */ 24 | public class FmtFunctionList extends GoExpressionNode{ 25 | 26 | private final Map functions = new HashMap<>(); 27 | private final GoLanguage language; 28 | 29 | public FmtFunctionList(GoLanguage language){ 30 | this.language = language; 31 | installBuiltins(); 32 | } 33 | 34 | public GoFunction register(String name, GoRootNode rootNode) { 35 | GoFunction function = new GoFunction(language,name); 36 | RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode); 37 | function.setCallTarget(callTarget); 38 | functions.put(name, function); 39 | return function; 40 | } 41 | 42 | public void installBuiltins(){ 43 | GoExpressionNode bodyNode = GoFmtPrintln.getFmtPrintln(); 44 | String name = GoContext.lookupNodeInfo(bodyNode.getClass()).shortName(); 45 | GoRootNode rootNode = new GoRootNode(language, new FrameDescriptor(), null, bodyNode, null, name); 46 | register(name,rootNode); 47 | } 48 | 49 | public GoFunction getFunction(String name){ 50 | GoFunction result = functions.get(name); 51 | if(result == null){ 52 | throw new GoException("Undefined: fmt."+name); 53 | } 54 | return result; 55 | } 56 | 57 | @Override 58 | public Object executeGeneric(VirtualFrame frame) { 59 | return this; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoNotEqualNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import java.util.List; 4 | 5 | import com.oracle.app.nodes.GoBinaryNode; 6 | import com.oracle.app.nodes.types.GoArray; 7 | import com.oracle.app.nodes.types.GoStruct; 8 | import com.oracle.app.runtime.GoFunction; 9 | import com.oracle.app.runtime.GoNull; 10 | import com.oracle.truffle.api.dsl.Specialization; 11 | import com.oracle.truffle.api.nodes.NodeInfo; 12 | import com.oracle.truffle.api.object.DynamicObject; 13 | import com.oracle.truffle.api.object.Shape; 14 | 15 | @NodeInfo(shortName = "!=") 16 | public abstract class GoNotEqualNode extends GoBinaryNode { 17 | 18 | @Specialization 19 | protected boolean notEqual(int left, int right) { 20 | return left != right; 21 | } 22 | 23 | @Specialization 24 | protected boolean notEqual(boolean left, boolean right) { 25 | return left != right; 26 | } 27 | 28 | @Specialization 29 | protected boolean notEqual(String left, String right) { 30 | return !left.equals(right); 31 | } 32 | 33 | @Specialization 34 | protected boolean notEqual(GoArray left, GoArray right){ 35 | //Need to be equal type 36 | if(!left.compare(right)){ 37 | return false; 38 | } 39 | //Then check each value 40 | for(int i = 0; i < left.len(); i++){ 41 | if(left.read(i) != right.read(i)){ 42 | return true; 43 | } 44 | } 45 | return false; 46 | } 47 | 48 | @Specialization 49 | protected boolean notEqual(DynamicObject left, DynamicObject right){ 50 | Shape leftshape = left.getShape(); 51 | Shape rightshape = right.getShape(); 52 | if(leftshape != rightshape){ 53 | return false; 54 | } 55 | List keylist = leftshape.getKeyList(GoStruct.getKeyList()); 56 | for(Object key : keylist){ 57 | if(left.get(key) != right.get(key)){ 58 | return true; 59 | } 60 | } 61 | return false; 62 | } 63 | 64 | @Specialization 65 | protected boolean notEqual(GoFunction left, GoFunction right) { 66 | return left != right; 67 | } 68 | 69 | @Specialization 70 | protected boolean notEqual(GoNull left, GoNull right) { 71 | return left != right; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoSliceExprNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.local.GoReadLocalVariableNode; 5 | import com.oracle.app.nodes.types.GoArrayLikeTypes; 6 | import com.oracle.app.nodes.types.GoSlice; 7 | import com.oracle.truffle.api.frame.FrameSlot; 8 | import com.oracle.truffle.api.frame.VirtualFrame; 9 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 10 | 11 | /** 12 | * Creates a {@link GoSlice} node from either a {@link GoSlice} or a {@link GoArray} 13 | * @author Trevor 14 | * 15 | */ 16 | public class GoSliceExprNode extends GoExpressionNode { 17 | 18 | GoReadLocalVariableNode expr; 19 | GoExpressionNode low; 20 | GoExpressionNode high; 21 | GoExpressionNode max; 22 | 23 | public GoSliceExprNode(GoReadLocalVariableNode expr, GoExpressionNode low, GoExpressionNode high, GoExpressionNode max) { 24 | this.expr = expr; 25 | this.low = low; 26 | this.high = high; 27 | this.max = max; 28 | } 29 | 30 | //TODO Could probably do specialization on null values and change the exceptions into goexceptions 31 | @Override 32 | public Object executeGeneric(VirtualFrame frame) { 33 | GoArrayLikeTypes array = (GoArrayLikeTypes) expr.executeGeneric(frame); 34 | int lowerbound = 0; 35 | int highbound = 0; 36 | int maxsize = 0; 37 | if(low == null){ 38 | lowerbound = array.lowerBound(); 39 | } 40 | else{ 41 | try { 42 | lowerbound = low.executeInteger(frame) + array.lowerBound(); 43 | } catch (UnexpectedResultException e) { 44 | e.printStackTrace(); 45 | } 46 | } 47 | if(high == null){ 48 | highbound = array.len() + array.lowerBound(); 49 | } 50 | else{ 51 | try { 52 | highbound = high.executeInteger(frame) + array.lowerBound(); 53 | } catch (UnexpectedResultException e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | if(max == null){ 58 | maxsize = array.cap(); 59 | } 60 | else{ 61 | try { 62 | maxsize = max.executeInteger(frame); 63 | } catch (UnexpectedResultException e) { 64 | e.printStackTrace(); 65 | } 66 | } 67 | GoSlice slice = GoSlice.createGoSlice(array,lowerbound,highbound,maxsize); 68 | return slice; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/parser/ir/nodes/GoIRBasicLitNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.parser.ir.nodes; 2 | 3 | import com.oracle.app.parser.ir.GoBaseIRNode; 4 | import com.oracle.truffle.api.source.Source; 5 | import com.oracle.truffle.api.source.SourceSection; 6 | 7 | /** 8 | * Abstract BasicLiteral node class for any basic type node. 9 | * Create basic IR nodes using the static createBasicLit method so that the 10 | * type handling is dealt with early on. When creating Truffle Nodes the type can 11 | * be created in one line. 12 | * Missing type: 13 | * FLOAT 14 | * IMAG 15 | * CHAR 16 | * @author Trevor 17 | * 18 | */ 19 | public abstract class GoIRBasicLitNode extends GoIRTypes { 20 | 21 | String type; 22 | String source; 23 | String val; 24 | 25 | public GoIRBasicLitNode(String source, String type, String value) { 26 | super("Basic Lit Node", type, value); 27 | this.source = source; 28 | this.type = type; 29 | } 30 | 31 | public String getType() { 32 | return type; 33 | } 34 | //TODO GET RID OF THIS, ITS USED ONCE IN PARSER AND I DONT EVEN LIKE THAT METHOD THAT I MADE 35 | public abstract String getValString(); 36 | 37 | /** 38 | * Create a source section based off of the source file. In theory that should be the actual Gofile. 39 | * Split the source by the colon and the first index is the line number 40 | * The charindex is the right hand side. Might need to change it so that it can capture the whole number 41 | * @param section The source code file 42 | * @return The created source section 43 | */ 44 | public abstract SourceSection getSource(Source section); 45 | 46 | /* 47 | * TODO Make the switch case on name.toLower() or seomthing like that 48 | */ 49 | public static GoIRBasicLitNode createBasicLit(String name, String value,String source){ 50 | switch(name.toLowerCase()){ 51 | case "int": 52 | return new GoIRIntNode(value,source); 53 | case "string": 54 | return new GoIRStringNode(value,source); 55 | case "float": 56 | case "float32": 57 | return new GoIRFloat32Node(value, source); 58 | case "float64": 59 | return new GoIRFloat64Node(value, source); 60 | default: 61 | System.out.println("Unimplemented Basic Lit type: " + name); 62 | return null; 63 | } 64 | 65 | } 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/GoPrintlnBuiltin.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins; 2 | 3 | import java.io.PrintWriter; 4 | 5 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 6 | import com.oracle.truffle.api.dsl.Specialization; 7 | import com.oracle.truffle.api.nodes.NodeInfo; 8 | 9 | @NodeInfo(shortName = "println") 10 | public abstract class GoPrintlnBuiltin extends GoBuiltinNode { 11 | 12 | @Specialization 13 | public int println(int value) { 14 | System.out.println(value); 15 | return value; 16 | } 17 | 18 | @TruffleBoundary 19 | private static void doPrint(PrintWriter out, int value){ 20 | out.println(value); 21 | } 22 | 23 | @Specialization 24 | public long println(long value) { 25 | doPrint(getContext().getOutput(), value); 26 | return value; 27 | } 28 | 29 | @Specialization 30 | public float println(float value) { 31 | System.out.printf("%+e\n", value); 32 | return value; 33 | } 34 | 35 | @Specialization 36 | public double println(double value) { 37 | System.out.printf("%+e\n", value); 38 | return value; 39 | } 40 | 41 | @TruffleBoundary 42 | private static void doPrint(PrintWriter out, long value) { 43 | out.println(value); 44 | } 45 | 46 | @Specialization 47 | public boolean println(boolean value) { 48 | doPrint(getContext().getOutput(), value); 49 | return value; 50 | } 51 | 52 | @TruffleBoundary 53 | private static void doPrint(PrintWriter out, boolean value) { 54 | out.println(value); 55 | } 56 | 57 | @Specialization 58 | public String println(String value) { 59 | doPrint(getContext().getOutput(), value); 60 | return value; 61 | } 62 | 63 | @TruffleBoundary 64 | private static void doPrint(PrintWriter out, String value) { 65 | out.println(value); 66 | } 67 | 68 | @Specialization 69 | public Object println(Object value) { 70 | doPrint(getContext().getOutput(), value); 71 | return value; 72 | } 73 | 74 | @TruffleBoundary 75 | private static void doPrint(PrintWriter out, Object value) { 76 | out.println(value); 77 | } 78 | 79 | @Override 80 | public String toString() { 81 | return "Println Builtin"; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/builtins/GoAppendBuiltin.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.builtins; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.types.GoArray.GoFloat32Array; 5 | import com.oracle.app.nodes.types.GoArray.GoFloat64Array; 6 | import com.oracle.app.nodes.types.GoArray.GoIntArray; 7 | import com.oracle.app.nodes.types.GoArray.GoObjectArray; 8 | import com.oracle.app.nodes.types.GoArray.GoStringArray; 9 | import com.oracle.app.nodes.types.GoPrimitiveTypes; 10 | import com.oracle.app.nodes.types.GoSlice; 11 | import com.oracle.app.nodes.types.GoSlice.GoFloat32Slice; 12 | import com.oracle.app.nodes.types.GoSlice.GoFloat64Slice; 13 | import com.oracle.app.nodes.types.GoSlice.GoIntSlice; 14 | import com.oracle.app.nodes.types.GoSlice.GoObjectSlice; 15 | import com.oracle.app.nodes.types.GoSlice.GoStringSlice; 16 | import com.oracle.truffle.api.frame.VirtualFrame; 17 | import com.oracle.truffle.api.nodes.NodeInfo; 18 | 19 | @NodeInfo(shortName = "append") 20 | public class GoAppendBuiltin extends GoExpressionNode { 21 | 22 | @Override 23 | public Object executeGeneric(VirtualFrame frame) { 24 | if(frame.getArguments().length < 1){ 25 | return null; 26 | } 27 | Object[] arguments = frame.getArguments(); 28 | GoSlice originalslice = (GoSlice) arguments[0]; 29 | GoSlice slice = createSliceCopy(originalslice.getType()); 30 | for(int i = 0; i < arguments.length; i++){ 31 | if(arguments[i] instanceof GoSlice){ 32 | GoSlice appendSlice = (GoSlice) arguments[i]; 33 | for(int j = 0; j < appendSlice.len(); j++){ 34 | slice.insert(appendSlice.read(j)); 35 | } 36 | } 37 | else{ 38 | slice.insert(arguments[i]); 39 | } 40 | } 41 | return slice; 42 | } 43 | 44 | private GoSlice createSliceCopy(GoPrimitiveTypes type) { 45 | switch(type){ 46 | case FLOAT32: 47 | return new GoFloat32Slice(new GoFloat32Array(0),0,0,0); 48 | case FLOAT64: 49 | return new GoFloat64Slice(new GoFloat64Array(0),0,0,0); 50 | case INT: 51 | return new GoIntSlice(new GoIntArray(0),0,0,0); 52 | case OBJECT: 53 | return new GoObjectSlice(new GoObjectArray(0),0,0,0); 54 | case STRING: 55 | return new GoStringSlice(new GoStringArray(0),0,0,0); 56 | case BOOL: 57 | } 58 | return null; 59 | } 60 | 61 | public static GoAppendBuiltin getAppendBuiltin(){ 62 | return new GoAppendBuiltin(); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/interop/GoForeignToGoTypeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.interop; 2 | 3 | import com.oracle.app.nodes.GoTypes; 4 | import com.oracle.app.runtime.GoContext; 5 | import com.oracle.app.runtime.GoNull; 6 | import com.oracle.truffle.api.CompilerDirectives; 7 | import com.oracle.truffle.api.dsl.Specialization; 8 | import com.oracle.truffle.api.dsl.TypeSystemReference; 9 | import com.oracle.truffle.api.interop.ForeignAccess; 10 | import com.oracle.truffle.api.interop.Message; 11 | import com.oracle.truffle.api.interop.TruffleObject; 12 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 13 | import com.oracle.truffle.api.nodes.Node; 14 | 15 | @TypeSystemReference(GoTypes.class) 16 | public abstract class GoForeignToGoTypeNode extends Node{ 17 | 18 | public abstract Object executeConvert(Object value); 19 | 20 | @Specialization 21 | protected static Object fromObject(Number value){ 22 | return GoContext.fromForeignValue(value); 23 | } 24 | 25 | @Specialization 26 | protected static Object fromString(String value){ 27 | return value; 28 | } 29 | 30 | @Specialization 31 | protected static Object fromBoolean(boolean value){ 32 | return value; 33 | } 34 | 35 | @Specialization(guards = "isBoxedPrimitive(value)") 36 | public Object unbox(TruffleObject value){ 37 | Object unboxed = doUnbox(value); 38 | return GoContext.fromForeignValue(unboxed); 39 | } 40 | 41 | @Specialization(guards = "!isBoxedPrimitive(value)") 42 | public Object fromTruffleObject(TruffleObject value){ 43 | return value; 44 | } 45 | 46 | @Child private Node isBoxed; 47 | 48 | protected final boolean isBoxedPrimitive(TruffleObject object){ 49 | if(isBoxed == null){ 50 | CompilerDirectives.transferToInterpreterAndInvalidate(); 51 | isBoxed = insert(Message.IS_BOXED.createNode()); 52 | } 53 | return ForeignAccess.sendIsBoxed(isBoxed, object); 54 | } 55 | 56 | @Child private Node unbox; 57 | 58 | protected final Object doUnbox(TruffleObject value){ 59 | if(unbox == null){ 60 | CompilerDirectives.transferToInterpreterAndInvalidate(); 61 | unbox = insert(Message.UNBOX.createNode()); 62 | } 63 | try{ 64 | return ForeignAccess.sendUnbox(unbox, value); 65 | } catch(UnsupportedMessageException e){ 66 | return GoNull.SINGLETON; 67 | } 68 | } 69 | 70 | public static GoForeignToGoTypeNode create(){ 71 | return GoForeignToGoTypeNodeGen.create(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoIfStmtNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoStatementNode; 5 | import com.oracle.truffle.api.dsl.UnsupportedSpecializationException; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | import com.oracle.truffle.api.nodes.Node; 8 | import com.oracle.truffle.api.nodes.NodeInfo; 9 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 10 | import com.oracle.truffle.api.profiles.ConditionProfile; 11 | 12 | @NodeInfo(shortName = "if", description = "The node implementing a conditional statement") 13 | public final class GoIfStmtNode extends GoStatementNode { 14 | 15 | @Child private GoStatementNode init; 16 | 17 | @Child private GoExpressionNode conditionNode; 18 | 19 | @Child private GoStatementNode thenNode; 20 | 21 | @Child private GoStatementNode elseNode; 22 | 23 | /** 24 | * Profiling information, collected by the interpreter, capturing the profiling information of 25 | * the condition. This allows the compiler to generate better code for conditions that are 26 | * always true or always false. Additionally the {@link CountingConditionProfile} implementation 27 | * (as opposed to {@link BinaryConditionProfile} implementation) transmits the probability of 28 | * the condition to be true to the compiler. 29 | */ 30 | private final ConditionProfile condition = ConditionProfile.createCountingProfile(); 31 | 32 | public GoIfStmtNode(GoStatementNode init, GoExpressionNode conditionNode, GoStatementNode thenNode, GoStatementNode elseNode) { 33 | this.init = init; 34 | this.conditionNode = conditionNode; 35 | this.thenNode = thenNode; 36 | this.elseNode = elseNode; 37 | } 38 | 39 | @Override 40 | public void executeVoid(VirtualFrame frame) { 41 | if(init != null) 42 | init.executeVoid(frame); 43 | if (condition.profile(evaluateCondition(frame))) { 44 | thenNode.executeVoid(frame); 45 | } else { 46 | if (elseNode != null) { 47 | elseNode.executeVoid(frame); 48 | } 49 | } 50 | } 51 | 52 | private boolean evaluateCondition(VirtualFrame frame) { 53 | try { 54 | return conditionNode.executeBoolean(frame); 55 | } catch (UnexpectedResultException ex) { 56 | throw new UnsupportedSpecializationException(this, new Node[]{conditionNode}, ex.getResult()); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoForWhileNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoStatementNode; 5 | import com.oracle.truffle.api.dsl.UnsupportedSpecializationException; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | import com.oracle.truffle.api.nodes.Node; 8 | import com.oracle.truffle.api.nodes.RepeatingNode; 9 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 10 | import com.oracle.truffle.api.profiles.BranchProfile; 11 | import com.oracle.truffle.api.source.SourceSection; 12 | 13 | public class GoForWhileNode extends Node implements RepeatingNode { 14 | @Child private GoExpressionNode conditionNode; 15 | 16 | @Child private GoStatementNode bodyNode; 17 | 18 | 19 | private final BranchProfile continueTaken = BranchProfile.create(); 20 | private final BranchProfile breakTaken = BranchProfile.create(); 21 | 22 | private SourceSection sourceSection; 23 | 24 | public GoForWhileNode(GoExpressionNode conditionNode, GoStatementNode bodyNode) { 25 | this.conditionNode = conditionNode; 26 | this.bodyNode = bodyNode; 27 | } 28 | 29 | @Override 30 | public SourceSection getSourceSection() { 31 | return sourceSection; 32 | } 33 | 34 | public void setSourceSection(SourceSection section) { 35 | assert this.sourceSection == null : "overwriting existing SourceSection"; 36 | this.sourceSection = section; 37 | } 38 | 39 | @Override 40 | public boolean executeRepeating(VirtualFrame frame) { 41 | if (!evaluateCondition(frame)) { 42 | return false; 43 | } 44 | 45 | try { 46 | bodyNode.executeVoid(frame); 47 | return true; 48 | 49 | } catch (GoContinueException ex) { 50 | continueTaken.enter(); 51 | 52 | return true; 53 | 54 | } catch (GoBreakException ex) { 55 | breakTaken.enter(); 56 | 57 | return false; 58 | } 59 | } 60 | 61 | private boolean evaluateCondition(VirtualFrame frame) { 62 | try { 63 | 64 | return conditionNode.executeBoolean(frame); 65 | } catch (UnexpectedResultException ex) { 66 | 67 | throw new UnsupportedSpecializationException(this, new Node[]{conditionNode}, ex.getResult()); 68 | } 69 | } 70 | 71 | @Override 72 | public String toString() { 73 | return GoStatementNode.formatSourceSection(this); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoShortCircuitNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.truffle.api.dsl.UnsupportedSpecializationException; 5 | import com.oracle.truffle.api.frame.VirtualFrame; 6 | import com.oracle.truffle.api.nodes.Node; 7 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 8 | import com.oracle.truffle.api.profiles.ConditionProfile; 9 | 10 | public abstract class GoShortCircuitNode extends GoExpressionNode { 11 | 12 | @Child private GoExpressionNode left; 13 | @Child private GoExpressionNode right; 14 | 15 | /** 16 | * Short circuits might be used just like a conditional statement it makes sense to profile the 17 | * branch probability. 18 | */ 19 | private final ConditionProfile evaluateRightProfile = ConditionProfile.createCountingProfile(); 20 | 21 | public GoShortCircuitNode(GoExpressionNode left, GoExpressionNode right) { 22 | this.left = left; 23 | this.right = right; 24 | } 25 | 26 | @Override 27 | public final Object executeGeneric(VirtualFrame frame) { 28 | return executeBoolean(frame); 29 | } 30 | 31 | @Override 32 | public final boolean executeBoolean(VirtualFrame frame) { 33 | boolean leftValue; 34 | try { 35 | leftValue = left.executeBoolean(frame); 36 | } catch (UnexpectedResultException e) { 37 | throw new UnsupportedSpecializationException(this, new Node[]{left, right}, new Object[]{e.getResult(), null}); 38 | } 39 | boolean rightValue; 40 | try { 41 | if (evaluateRightProfile.profile(isEvaluateRight(leftValue))) { 42 | rightValue = right.executeBoolean(frame); 43 | } else { 44 | rightValue = false; 45 | } 46 | } catch (UnexpectedResultException e) { 47 | throw new UnsupportedSpecializationException(this, new Node[]{left, right}, new Object[]{leftValue, e.getResult()}); 48 | } 49 | return execute(leftValue, rightValue); 50 | } 51 | 52 | /** 53 | * This method is called after the left child was evaluated, but before the right child is 54 | * evaluated. The right child is only evaluated when the return value is {code true}. 55 | */ 56 | protected abstract boolean isEvaluateRight(boolean leftValue); 57 | 58 | /** 59 | * Calculates the result of the short circuit operation. If the right node is not evaluated then 60 | * false is provided. 61 | */ 62 | protected abstract boolean execute(boolean leftValue, boolean rightValue); 63 | 64 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoCaseClauseNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoArrayExprNode; 4 | import com.oracle.app.nodes.GoExpressionNode; 5 | import com.oracle.app.nodes.GoStatementNode; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | import com.oracle.truffle.api.nodes.NodeInfo; 8 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 9 | 10 | @NodeInfo(shortName = "case clause", description = "A node implementing a single case in a switch statement") 11 | public final class GoCaseClauseNode extends GoStatementNode { 12 | 13 | @Children private final GoExpressionNode[] list; 14 | @Children private final GoStatementNode[] body; 15 | public String caseType = "case"; 16 | 17 | public GoCaseClauseNode(GoArrayExprNode list, GoStatementNode[] body) { 18 | this.body = body; 19 | if(list == null){ 20 | caseType = "default"; 21 | this.list = null; 22 | } 23 | else{ 24 | this.list = list.getArguments(); 25 | } 26 | } 27 | 28 | /** 29 | * It checks all expressions in this case (could be multiple for a single case). 30 | * If match, execute its statement nodes then return true so BlockNode can break. 31 | * Else return false so BlockNode can execute the next case. 32 | * 33 | * @param frame: Virtual frame 34 | * @param value: Value of the tag passed from SwitchStatement and the block statement. 35 | */ 36 | public boolean caseExecute(VirtualFrame frame, Object value){ 37 | for (GoExpressionNode caseListElem : list) { 38 | if (caseListElem.executeGeneric(frame) == value) { 39 | for (GoStatementNode executeBody : body) { 40 | executeBody.executeVoid(frame); 41 | } 42 | return true; 43 | } 44 | } 45 | return false; 46 | } 47 | 48 | public boolean ifElseExecute(VirtualFrame frame){ 49 | for (GoExpressionNode caseListElem : list) { 50 | //Should probably look at this one day 51 | try { 52 | if (caseListElem.executeBoolean(frame)){ 53 | for (GoStatementNode executeBody : body){ 54 | executeBody.executeVoid(frame); 55 | } 56 | return true; 57 | } 58 | } catch (UnexpectedResultException e) { 59 | return false; 60 | } 61 | } 62 | return false; 63 | } 64 | 65 | @Override 66 | public void executeVoid(VirtualFrame frame) { 67 | for(GoStatementNode node : body){ 68 | node.executeVoid(frame); 69 | } 70 | 71 | } 72 | } 73 | 74 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/expression/GoEqualNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.expression; 2 | 3 | import java.util.List; 4 | 5 | import com.oracle.app.nodes.GoBinaryNode; 6 | import com.oracle.app.nodes.types.GoArray; 7 | import com.oracle.app.nodes.types.GoStruct; 8 | import com.oracle.app.runtime.GoFunction; 9 | import com.oracle.app.runtime.GoNull; 10 | import com.oracle.truffle.api.dsl.Specialization; 11 | import com.oracle.truffle.api.nodes.NodeInfo; 12 | import com.oracle.truffle.api.object.DynamicObject; 13 | import com.oracle.truffle.api.object.Shape; 14 | 15 | @NodeInfo(shortName = "==") 16 | public abstract class GoEqualNode extends GoBinaryNode { 17 | 18 | @Specialization 19 | protected boolean equal(int left, int right) { 20 | return left == right; 21 | } 22 | 23 | @Specialization 24 | protected boolean equal(double left, double right) { 25 | return left == right; 26 | } 27 | 28 | @Specialization 29 | protected boolean equal(boolean left, boolean right) { 30 | return left == right; 31 | } 32 | 33 | @Specialization 34 | protected boolean equal(String left, String right) { 35 | return left.equals(right); 36 | } 37 | 38 | @Specialization 39 | protected boolean equal(GoFunction left, GoFunction right) { 40 | /* 41 | * TODO Check if we compare function pointers??? 42 | */ 43 | return left == right; 44 | } 45 | 46 | @Specialization 47 | protected boolean equal(GoArray left, GoArray right){ 48 | //Need to be equal type 49 | if(!left.compare(right)){ 50 | return false; 51 | } 52 | //Then check each value 53 | for(int i = 0; i < left.len(); i++){ 54 | if(left.read(i) != right.read(i)){ 55 | return false; 56 | } 57 | } 58 | return true; 59 | } 60 | 61 | @Specialization 62 | protected boolean equal(DynamicObject left, DynamicObject right){ 63 | Shape leftshape = left.getShape(); 64 | Shape rightshape = right.getShape(); 65 | if(leftshape != rightshape){ 66 | return false; 67 | } 68 | List keylist = leftshape.getKeyList(GoStruct.getKeyList()); 69 | for(Object key : keylist){ 70 | if(left.get(key) != right.get(key)){ 71 | return false; 72 | } 73 | } 74 | return true; 75 | } 76 | 77 | @Specialization 78 | protected boolean equal(GoNull left, GoNull right) { 79 | return left == right; 80 | } 81 | 82 | @Specialization(guards = "left.getClass() != right.getClass()") 83 | protected boolean equal(Object left, Object right) { 84 | assert !left.equals(right); 85 | return false; 86 | } 87 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/call/GoInvokeNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.call; 2 | 3 | import java.util.Arrays; 4 | 5 | import com.oracle.app.nodes.GoExpressionNode; 6 | import com.oracle.app.nodes.SpecDecl.GoSelectorExprNode; 7 | import com.oracle.truffle.api.CompilerAsserts; 8 | import com.oracle.truffle.api.frame.VirtualFrame; 9 | import com.oracle.truffle.api.instrumentation.StandardTags; 10 | import com.oracle.truffle.api.nodes.ExplodeLoop; 11 | import com.oracle.truffle.api.nodes.NodeInfo; 12 | import com.oracle.truffle.api.object.DynamicObject; 13 | 14 | @NodeInfo(shortName = "invoke") 15 | public class GoInvokeNode extends GoExpressionNode { 16 | 17 | @Child protected GoExpressionNode functionNode; 18 | @Children protected final GoExpressionNode[] argumentNodes; 19 | @Child protected GoGenericDispatchNode dispatchNode; 20 | 21 | 22 | public GoInvokeNode(GoExpressionNode functionNode, GoExpressionNode[] argumentNodes) { 23 | this.functionNode = functionNode; 24 | this.argumentNodes = argumentNodes; 25 | this.dispatchNode = GoGenericDispatchNodeGen.create(); 26 | } 27 | 28 | @ExplodeLoop 29 | @Override 30 | public Object executeGeneric(VirtualFrame frame) { 31 | Object function = functionNode.executeGeneric(frame); 32 | 33 | CompilerAsserts.compilationConstant(argumentNodes.length); 34 | Object[] argumentValues = new Object[argumentNodes.length]; 35 | for (int i = 0; i < argumentNodes.length; i++) { 36 | argumentValues[i] = argumentNodes[i].executeGeneric(frame); 37 | } 38 | 39 | //This is used for struct methods. The struct passes itself as an argument to the method, but this is 40 | // a bad way of passing the struct to the function arguments. 41 | //TODO This should not be here. Think of a better place to insert the struct object into the arguments 42 | if(functionNode instanceof GoSelectorExprNode){ 43 | Object selector = ((GoSelectorExprNode) functionNode).getSelector(frame); 44 | if(selector instanceof DynamicObject){ 45 | Object[] newargvalues = Arrays.copyOf(argumentValues, argumentNodes.length+1); 46 | newargvalues[argumentNodes.length] = selector; 47 | argumentValues = newargvalues; 48 | } 49 | } 50 | 51 | return dispatchNode.executeDispatch(function, argumentValues); 52 | } 53 | 54 | @Override 55 | protected boolean isTaggedWith(Class tag) { 56 | if (tag == StandardTags.CallTag.class) { 57 | return true; 58 | } 59 | return super.isTaggedWith(tag); 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/nodes/controlflow/GoForRepeatingNode.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.nodes.controlflow; 2 | 3 | import com.oracle.app.nodes.GoExpressionNode; 4 | import com.oracle.app.nodes.GoStatementNode; 5 | import com.oracle.truffle.api.dsl.UnsupportedSpecializationException; 6 | import com.oracle.truffle.api.frame.VirtualFrame; 7 | import com.oracle.truffle.api.nodes.Node; 8 | import com.oracle.truffle.api.nodes.RepeatingNode; 9 | import com.oracle.truffle.api.nodes.UnexpectedResultException; 10 | import com.oracle.truffle.api.profiles.BranchProfile; 11 | import com.oracle.truffle.api.source.SourceSection; 12 | 13 | public class GoForRepeatingNode extends Node implements RepeatingNode { 14 | @Child private GoExpressionNode conditionNode; 15 | 16 | @Child private GoExpressionNode post; 17 | 18 | @Child private GoStatementNode bodyNode; 19 | 20 | 21 | private final BranchProfile continueTaken = BranchProfile.create(); 22 | private final BranchProfile breakTaken = BranchProfile.create(); 23 | 24 | private SourceSection sourceSection; 25 | 26 | public GoForRepeatingNode(GoExpressionNode conditionNode, GoExpressionNode post, GoStatementNode bodyNode) { 27 | this.conditionNode = conditionNode; 28 | this.post = post; 29 | this.bodyNode = bodyNode; 30 | } 31 | 32 | @Override 33 | public SourceSection getSourceSection() { 34 | return sourceSection; 35 | } 36 | 37 | public void setSourceSection(SourceSection section) { 38 | assert this.sourceSection == null : "overwriting existing SourceSection"; 39 | this.sourceSection = section; 40 | } 41 | 42 | @Override 43 | public boolean executeRepeating(VirtualFrame frame) { 44 | if (!evaluateCondition(frame)) { 45 | return false; 46 | } 47 | 48 | try { 49 | bodyNode.executeVoid(frame); 50 | post.executeVoid(frame); 51 | return true; 52 | 53 | } catch (GoContinueException ex) { 54 | continueTaken.enter(); 55 | 56 | return true; 57 | 58 | } catch (GoBreakException ex) { 59 | breakTaken.enter(); 60 | 61 | return false; 62 | } 63 | } 64 | 65 | private boolean evaluateCondition(VirtualFrame frame) { 66 | try { 67 | 68 | return conditionNode.executeBoolean(frame); 69 | } catch (UnexpectedResultException ex) { 70 | 71 | throw new UnsupportedSpecializationException(this, new Node[]{conditionNode}, ex.getResult()); 72 | } 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return GoStatementNode.formatSourceSection(this); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /golang/src/main/java/com/oracle/app/runtime/GoFunctionRegistry.java: -------------------------------------------------------------------------------- 1 | package com.oracle.app.runtime; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | import com.oracle.app.GoLanguage; 11 | import com.oracle.app.nodes.GoRootNode; 12 | import com.oracle.truffle.api.RootCallTarget; 13 | import com.oracle.truffle.api.Truffle; 14 | 15 | /** 16 | * Manages the mapping from function names to {@link GoFunction function objects}. 17 | */ 18 | public final class GoFunctionRegistry { 19 | 20 | private final GoLanguage language; 21 | private final Map functions = new HashMap<>(); 22 | 23 | public GoFunctionRegistry(GoLanguage language) { 24 | this.language = language; 25 | } 26 | 27 | /** 28 | * Returns the canonical {@link GoFunction} object for the given name. If it does not exist yet, 29 | * it is created. 30 | */ 31 | public GoFunction lookup(String name, boolean createIfNotPresent) { 32 | GoFunction result = functions.get(name); 33 | if (result == null && createIfNotPresent) { 34 | result = new GoFunction(language, name); 35 | functions.put(name, result); 36 | } 37 | return result; 38 | } 39 | 40 | /** 41 | * Associates the {@link GoFunction} with the given name with the given implementation root 42 | * node. If the function did not exist before, it defines the function. If the function existed 43 | * before, it redefines the function and the old implementation is discarded. 44 | */ 45 | public GoFunction register(String name, GoRootNode rootNode) { 46 | GoFunction function = lookup(name, true); 47 | RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode); 48 | function.setCallTarget(callTarget); 49 | return function; 50 | } 51 | 52 | public void register(Map newFunctions) { 53 | for (Map.Entry entry : newFunctions.entrySet()) { 54 | register(entry.getKey(), entry.getValue()); 55 | } 56 | } 57 | 58 | /*Used to redefine builtin functions 59 | public void register(Source newFunctions) { 60 | register(Parser.parseGo(language, newFunctions)); 61 | } 62 | */ 63 | /** 64 | * Returns the sorted list of all functions, for printing purposes only. 65 | */ 66 | public List getFunctions() { 67 | List result = new ArrayList<>(functions.values()); 68 | Collections.sort(result, new Comparator() { 69 | public int compare(GoFunction f1, GoFunction f2) { 70 | return f1.toString().compareTo(f2.toString()); 71 | } 72 | }); 73 | return result; 74 | } 75 | 76 | } --------------------------------------------------------------------------------