net.sourceforge.plantuml.jasic
Class Jasic
- java.lang.Object
-
- net.sourceforge.plantuml.jasic.Jasic
-
public class Jasic extends java.lang.ObjectThis defines a single class that contains an entire interpreter for a language very similar to the original BASIC. Everything is here (albeit in very simplified form): tokenizing, parsing, and interpretation. The file is organized in phases, with each appearing roughly in the order that they occur when a program is run. You should be able to read this top-down to walk through the entire process of loading and running a program. Jasic language syntax --------------------- Comments start with ' and proceed to the end of the line: print "hi there" ' this is a comment Numbers and strings are supported. Strings should be in "double quotes", and only positive integers can be parsed (though numbers are double internally). Variables are identified by name which must start with a letter and can contain letters or numbers. Case is significant for names and keywords. Each statement is on its own line. Optionally, a line may have a label before the statement. A label is a name that ends with a colon: foo: The following statements are supported:= Evaluates the expression and assigns the result to the given named variable. All variables are globally scoped. pi = (314159 / 10000) print Evaluates the expression and prints the result. print "hello, " + "world" input Reads in a line of input from the user and stores it in the variable with the given name. input guess goto
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description classJasic.AssignStatementAn assignment statement evaluates an expression and stores the result in a variable.static interfaceJasic.ExpressionBase interface for an expression.classJasic.GotoStatementA "goto" statement jumps execution to another place in the program.classJasic.IfThenStatementAn if then statement jumps execution to another place in the program, but only if an expression evaluates to something other than 0.classJasic.InputStatementAn "input" statement reads input from the user and stores it in a variable.classJasic.NumberValueA numeric value.classJasic.OperatorExpressionAn operator expression evaluates two expressions and then performs some arithmetic operation on the results.classJasic.PrintStatementA "print" statement evaluates an expression, converts the result to a string, and displays it to the user.static interfaceJasic.StatementBase interface for a Jasic statement.classJasic.StringValueA string value.static interfaceJasic.ValueThis is the base interface for a value.classJasic.VariableExpressionA variable expression evaluates to the current value stored in that variable.
-
Constructor Summary
Constructors Constructor and Description Jasic()Constructs a new Jasic instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidinterpret(java.lang.String source)This is where the magic happens.static voidmain(java.lang.String[] args)Runs the interpreter as a command-line app.
-
-
-
Constructor Detail
-
Jasic
public Jasic()
Constructs a new Jasic instance. The instance stores the global state of the interpreter such as the values of all of the variables and the current statement.
-
-
Method Detail
-
main
public static void main(java.lang.String[] args)
Runs the interpreter as a command-line app. Takes one argument: a path to a script file to load and run. The script should contain one statement per line.- Parameters:
args- Command-line arguments.
-
interpret
public void interpret(java.lang.String source)
This is where the magic happens. This runs the code through the parsing pipeline to generate the AST. Then it executes each statement. It keeps track of the current line in a member variable that the statement objects have access to. This lets "goto" and "if then" do flow control by simply setting the index of the current statement. In an interpreter that didn't mix the interpretation logic in with the AST node classes, this would be doing a lot more work.- Parameters:
source- A string containing the source code of a .jas script to interpret.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG