comparison graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/runtime/SLContext.java @ 13821:b16ec83edc73

Documentation and more refactoring of Simple Language
author Christian Wimmer <christian.wimmer@oracle.com>
date Wed, 29 Jan 2014 20:45:43 -0800
parents 7c418666c6c9
children 64c77f0577bb
comparison
equal deleted inserted replaced
13820:20e7727588e8 13821:b16ec83edc73
1 /* 1 /*
2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
23 package com.oracle.truffle.sl.runtime; 23 package com.oracle.truffle.sl.runtime;
24 24
25 import java.io.*; 25 import java.io.*;
26 26
27 import com.oracle.truffle.api.dsl.*; 27 import com.oracle.truffle.api.dsl.*;
28 import com.oracle.truffle.api.frame.*;
29 import com.oracle.truffle.api.nodes.*;
28 import com.oracle.truffle.api.source.*; 30 import com.oracle.truffle.api.source.*;
29 import com.oracle.truffle.sl.builtins.*; 31 import com.oracle.truffle.sl.builtins.*;
30 import com.oracle.truffle.sl.nodes.*; 32 import com.oracle.truffle.sl.nodes.*;
33 import com.oracle.truffle.sl.nodes.local.*;
34 import com.oracle.truffle.sl.parser.*;
31 35
36 /**
37 * The run-time state of SL during execution. One context is instantiated before any source code is
38 * parsed, and this context is passed around to all methods that need access to it. For example, the
39 * context is used during {@link SLNodeFactory parsing} and by {@link SLBuiltinNode#getContext()
40 * builtin functions}.
41 * <p>
42 * It would be an error to have two different context instances at the same. From a software
43 * engineering point of view, it is better to pass around this encapsulated context object instead
44 * of storing the data in static Java fields.
45 */
32 public final class SLContext { 46 public final class SLContext {
33 private final SourceManager sourceManager; 47 private final SourceManager sourceManager;
48 private final BufferedReader input;
34 private final PrintStream output; 49 private final PrintStream output;
35 private final SLFunctionRegistry functionRegistry; 50 private final SLFunctionRegistry functionRegistry;
36 51
37 public SLContext(SourceManager sourceManager, PrintStream output) { 52 public SLContext(SourceManager sourceManager, BufferedReader input, PrintStream output) {
38 this.sourceManager = sourceManager; 53 this.sourceManager = sourceManager;
54 this.input = input;
39 this.output = output; 55 this.output = output;
40 this.functionRegistry = new SLFunctionRegistry(); 56 this.functionRegistry = new SLFunctionRegistry();
41 57
42 installBuiltins(); 58 installBuiltins();
43 } 59 }
44 60
61 /**
62 * Returns the source manger that controls all SL source code that is executed.
63 */
45 public SourceManager getSourceManager() { 64 public SourceManager getSourceManager() {
46 return sourceManager; 65 return sourceManager;
47 } 66 }
48 67
49 public PrintStream getPrintOutput() { 68 /**
69 * Returns the default input, i.e., the source for the {@link SLReadlnBuiltin}. To allow unit
70 * testing, we do not use {@link System#in} directly.
71 */
72 public BufferedReader getInput() {
73 return input;
74 }
75
76 /**
77 * The default default, i.e., the output for the {@link SLPrintlnBuiltin}. To allow unit
78 * testing, we do not use {@link System#out} directly.
79 */
80 public PrintStream getOutput() {
50 return output; 81 return output;
51 } 82 }
52 83
84 /**
85 * Returns the registry of all functions that are currently defined.
86 */
53 public SLFunctionRegistry getFunctionRegistry() { 87 public SLFunctionRegistry getFunctionRegistry() {
54 return functionRegistry; 88 return functionRegistry;
55 } 89 }
56 90
91 /**
92 * Adds all builitin functions to the {@link SLFunctionRegistry}. This method lists all
93 * {@link SLBuiltinNode builtin implementation classes}.
94 */
57 private void installBuiltins() { 95 private void installBuiltins() {
58 installBuiltin(SLPrintBuiltinFactory.getInstance(), "print"); 96 installBuiltin(SLReadlnBuiltinFactory.getInstance());
59 installBuiltin(SLTimeBuiltinFactory.getInstance(), "time"); 97 installBuiltin(SLPrintlnBuiltinFactory.getInstance());
60 installBuiltin(SLDefineFunctionBuiltinFactory.getInstance(), "defineFunction"); 98 installBuiltin(SLNanoTimeBuiltinFactory.getInstance());
99 installBuiltin(SLDefineFunctionBuiltinFactory.getInstance());
61 } 100 }
62 101
63 private void installBuiltin(NodeFactory<? extends SLBuiltinNode> factory, String name) { 102 private void installBuiltin(NodeFactory<? extends SLBuiltinNode> factory) {
64 getFunctionRegistry().register(name, SLRootNode.createBuiltin(this, factory, name)); 103 /*
104 * The builtin node factory is a class that is automatically generated by the Truffle DSL.
105 * The signature returned by the factory reflects the signature of the @Specialization
106 * methods in the builtin classes.
107 */
108 int argumentCount = factory.getExecutionSignature().size();
109 SLExpressionNode[] argumentNodes = new SLExpressionNode[argumentCount];
110 /*
111 * Builtin functions are like normal functions, i.e., the arguments are passed in as an
112 * Object[] array encapsulated in SLArguments. A SLReadArgumentNode extracts a parameter
113 * from this array.
114 */
115 for (int i = 0; i < argumentCount; i++) {
116 argumentNodes[i] = new SLReadArgumentNode(i);
117 }
118 /* Instantiate the builtin node. This node performs the actual functionality. */
119 SLBuiltinNode builtinBodyNode = factory.createNode(argumentNodes, this);
120 /* The name of the builtin function is specified via an annotation on the node class. */
121 String name = builtinBodyNode.getClass().getAnnotation(NodeInfo.class).shortName();
122 /* Wrap the builtin in a RootNode. Truffle requires all AST to start with a RootNode. */
123 SLRootNode rootNode = new SLRootNode(new FrameDescriptor(), builtinBodyNode, name);
124
125 /* Register the builtin function in our function registry. */
126 getFunctionRegistry().register(name, rootNode);
65 } 127 }
66 } 128 }