6 people like it.

Small Basic Interpreter

Small Basic abstract syntax tree, interpreter and embedded DSL. Supports Small Basic's keywords and arithmetic, logical and comparison operators.

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
114: 
115: 
116: 
117: 
118: 
119: 
120: 
121: 
122: 
123: 
124: 
125: 
126: 
127: 
128: 
129: 
130: 
131: 
132: 
133: 
134: 
135: 
136: 
137: 
138: 
139: 
140: 
141: 
142: 
143: 
144: 
145: 
146: 
147: 
148: 
149: 
150: 
151: 
152: 
153: 
154: 
155: 
156: 
157: 
158: 
159: 
160: 
161: 
162: 
163: 
164: 
165: 
166: 
167: 
168: 
169: 
170: 
171: 
172: 
173: 
174: 
175: 
176: 
177: 
178: 
179: 
180: 
181: 
182: 
183: 
184: 
185: 
186: 
187: 
188: 
189: 
190: 
191: 
192: 
193: 
194: 
195: 
196: 
197: 
198: 
199: 
200: 
201: 
202: 
203: 
204: 
205: 
206: 
207: 
208: 
209: 
210: 
211: 
212: 
213: 
214: 
215: 
216: 
217: 
218: 
219: 
220: 
221: 
222: 
223: 
224: 
225: 
226: 
227: 
228: 
229: 
230: 
231: 
232: 
233: 
234: 
235: 
236: 
237: 
238: 
239: 
240: 
241: 
242: 
243: 
244: 
245: 
246: 
247: 
248: 
249: 
250: 
251: 
252: 
253: 
254: 
255: 
256: 
257: 
258: 
259: 
260: 
261: 
262: 
263: 
264: 
265: 
266: 
267: 
268: 
269: 
270: 
271: 
272: 
273: 
274: 
275: 
276: 
277: 
278: 
279: 
280: 
281: 
282: 
283: 
284: 
285: 
286: 
287: 
288: 
289: 
290: 
291: 
292: 
293: 
294: 
295: 
296: 
297: 
298: 
299: 
300: 
301: 
302: 
303: 
304: 
305: 
306: 
307: 
308: 
309: 
310: 
311: 
312: 
313: 
314: 
// Type abbreviations
type label = string
type identifier = string
type index = int
type MethodInfo = System.Reflection.MethodInfo

/// Small Basic arithmetic operation
type arithmetic = Add | Subtract | Multiply | Divide
/// Small Basic comparison operaton
type comparison = Eq | Ne | Lt | Gt | Le | Ge
/// Small Basic logical operation
type logical = And | Or
/// Small Basic value
type value =
    | Bool of bool
    | Int of int
    | Double of double
    | String of string
/// Small Basic expression
type expr =
    | Literal of value
    | Var of identifier
    | GetAt of location
    | Func of call
    | Neg of expr
    | Arithmetic of expr * arithmetic * expr
    | Comparison of expr * comparison * expr
    | Logical of expr * logical * expr
and location =
    | Location of identifier * expr[]
and call =
    | Call of MethodInfo * expr[]
/// Assignment
type assign =
    | Set of identifier * expr
/// Small Basic instruction
type instruction =
    | Assign of assign
    | SetAt of location * expr 
    | Action of call
    | For of assign * expr * expr
    | EndFor
    | If of expr
    | Else
    | EndIf
    | While of expr
    | EndWhile
    | Sub of identifier
    | EndSub
    | GoSub of identifier
    | Label of label
    | Goto of label

/// Converts value to obj
let fromObj (x:obj) =
    match x with
    | :? bool as x -> Bool x
    | :? int as x -> Int x
    | :? double as x -> Double x
    | :? string as x -> String x
    | null -> Int 0
    | x -> raise (new System.NotSupportedException(x.ToString()))
/// Converts value to obj
let toObj = function
    | Bool x -> box x
    | Int x -> box x
    | Double x -> box x
    | String x -> box x
/// Converts value to int
let toInt = function
    | Bool x -> raise (new System.NotSupportedException())
    | Int x -> x
    | Double x -> int x
    | String x -> int x
/// Converts value to bool
let toBool = function
    | Bool x -> x
    | _ -> raise (new System.NotSupportedException())
/// Coerces a tuple of numeric values to double
let (|AsDoubles|_|) = function
    | Double l, Double r -> Some(l,r)
    | Int l, Double r -> Some(double l,r)
    | Double l, Int r -> Some(l,double r)
    | _, _ -> None
/// Compares values
let compare lhs rhs =
    match lhs, rhs with
    | Bool l, Bool r -> l.CompareTo(r)
    | Int l, Int r -> l.CompareTo(r)
    | AsDoubles (l,r) -> l.CompareTo(r)
    | String l, String r -> l.CompareTo(r)
    | _ -> raise (new System.NotSupportedException(sprintf "%A %A" lhs rhs))

open System.Collections.Generic

type VarLookup = Dictionary<identifier,value>
type ArrayLookup = Dictionary<identifier,Dictionary<value,value>>

/// Evaluates expressions
let rec eval state (expr:expr) =
    let (vars:VarLookup), (arrays:ArrayLookup) = state
    match expr with
    | Literal x -> x
    | Var identifier -> vars.[identifier]
    | GetAt(Location(identifier,[|index|])) -> arrays.[identifier].[eval state index]
    | GetAt(Location(identifier,_)) -> raise (System.NotSupportedException())
    | Func(call) -> invoke state call
    | Neg x -> arithmetic (eval state x) Multiply (Int(-1))
    | Arithmetic(l,op,r) -> arithmetic (eval state l) op (eval state r)
    | Comparison(l,op,r) -> comparison (eval state l) op (eval state r)
    | Logical(l,op,r) -> logical (eval state l) op (eval state r)
and comparison lhs op rhs =
    let x = compare lhs rhs
    match op with
    | Eq -> x = 0   | Ne -> x <> 0
    | Lt -> x < 0   | Gt -> x > 0
    | Le -> x <= 0  | Ge -> x >= 0
    |> fromObj
and arithmetic lhs op rhs =
    match op, (lhs, rhs) with
    | Add, (Int l,Int r) -> Int(l + r)
    | Add, AsDoubles (l,r) -> Double(l + r)
    | Add, (String l, String r) -> String(l + r)
    | Subtract, (Int l,Int r) -> Int(l - r)
    | Subtract, AsDoubles (l,r) -> Double(l - r)
    | Multiply, (Int l,Int r) -> Int(l * r)
    | Multiply, AsDoubles (l,r) -> Double(l * r)
    | Divide, (Int l,Int r) -> Int(l - r)
    | Divide, AsDoubles (l,r) -> Double(l - r)
    | _ -> raise (System.NotImplementedException())
and logical lhs op rhs =
    match op, lhs, rhs with
    | And, Bool l, Bool r -> Bool(l && r)
    | Or, Bool l, Bool r -> Bool(l || r)
    | _, _, _ -> raise (System.NotImplementedException())
and invoke state (Call(mi,args)) =
    let args = args |> Array.map (eval state >> toObj)
    mi.Invoke(null, args) |> fromObj

/// Runs program
let run (program:instruction[]) =
    /// Program index
    let pi = ref 0
    /// Variable lookup   
    let variables = VarLookup()
    /// Array lookup
    let arrays = ArrayLookup()
    /// Current state
    let state = variables, arrays
    /// For from EndFor lookup
    let forLoops = Dictionary<index, index * identifier * expr * expr>()
    /// While from EndWhile lookup
    let whileLoops = Dictionary<index, index>()
    /// Call stack for Gosubs
    let callStack = Stack<index>()
    /// Evaluates expression with variables
    let eval = eval (variables,arrays)
    /// Assigns result of expression to variable
    let assign (Set(identifier,expr)) = variables.[identifier] <- eval expr
    /// Finds first index of instructions
    let findFirstIndex start (inc,dec) instructions =
        let mutable i = start
        let mutable nest = 0
        while nest > 0 || instructions |> List.exists ((=) program.[i]) |> not do 
            if inc program.[i] then nest <- nest + 1
            if nest > 0 && dec program.[i] then nest <- nest - 1
            i <- i + 1
        i
    /// Finds index of instruction
    let findIndex start (inc,dec) instruction =
        findFirstIndex start (inc,dec) [instruction]
    let isIf = function If(_) -> true | _ -> false
    let isEndIf = (=) EndIf
    let isFor = function For(_,_,_) -> true | _ -> false
    let isEndFor = (=) EndFor
    let isWhile = function While(_) -> true | _ -> false
    let isEndWhile = (=) EndWhile
    let isFalse _ = false
    /// Instruction step
    let step () =
        let instruction = program.[!pi]
        match instruction with
        | Assign(set) -> assign set
        | SetAt(Location(identifier,[|index|]),expr) ->
            let array = 
                match arrays.TryGetValue(identifier) with
                | true, array -> array
                | false, _ -> 
                    let array = Dictionary<value,value>()
                    arrays.Add(identifier,array)
                    array
            array.[eval index] <- eval expr
        | SetAt(Location(_,_),expr) -> raise (System.NotSupportedException())
        | Action(call) -> invoke state call |> ignore
        | If(condition) ->            
            if eval condition |> toBool |> not then
                let index = findFirstIndex (!pi+1) (isIf, isEndIf) [Else;EndIf]
                pi := index
        | Else ->
            let index = findIndex !pi (isIf,isEndIf) EndIf
            pi := index
        | EndIf -> ()
        | For((Set(identifier,expr) as from), target, step) ->
            assign from
            let index = findIndex (!pi+1) (isFor,isEndFor) EndFor
            forLoops.[index] <- (!pi, identifier, target, step)
            if toInt(variables.[identifier]) > toInt(eval target) 
            then pi := index
        | EndFor ->
            let start, identifier, target, step = forLoops.[!pi]
            let x = variables.[identifier]
            variables.[identifier] <- arithmetic x Add (eval step)
            if toInt(variables.[identifier]) <= toInt(eval target) 
            then pi := start
        | While condition ->
            let index = findIndex (!pi+1) (isWhile,isEndWhile) EndWhile
            whileLoops.[index] <- !pi 
            if eval condition |> toBool |> not then pi := index
        | EndWhile ->
            pi := whileLoops.[!pi] - 1
        | Sub(identifier) ->
            pi := findIndex (!pi+1) (isFalse, isFalse) EndSub
        | GoSub(identifier) ->
            let index = findIndex 0 (isFalse, isFalse) (Sub(identifier))
            callStack.Push(!pi)
            pi := index
        | EndSub ->
            pi := callStack.Pop()
        | Label(label) -> ()
        | Goto(label) -> pi := findIndex 0 (isFalse,isFalse) (Label(label))
    while !pi < program.Length do step (); incr pi

// Embedded DSL

let B x = Literal(Bool(x))
let I x = Literal(Int(x))
let D x = Literal(Double(x))
let S x = Literal(String(x))
let AT(name,expr) = Location(name,[|expr|])
let (.<-) (name:string) (expr:expr) = Assign(Set(name, expr))
let (.<!-) (location:location) (expr:expr) = SetAt(location,expr)
let (!) (name:string) = Var(name)
let (!!) (location:location) = GetAt(location)
let (.+) (lhs:expr) (rhs:expr) = Arithmetic(lhs, Add, rhs)
let (.-) (lhs:expr) (rhs:expr) = Arithmetic(lhs, Subtract, rhs)
let (.*) (lhs:expr) (rhs:expr) = Arithmetic(lhs, Multiply, rhs)
let (./) (lhs:expr) (rhs:expr) = Arithmetic(lhs, Divide, rhs)
let (.=) (lhs:expr) (rhs:expr) = Comparison(lhs, Eq, rhs)
let (.<>) (lhs:expr) (rhs:expr) = Comparison(lhs, Ne, rhs)
let (.<) (lhs:expr) (rhs:expr) = Comparison(lhs, Lt, rhs)
let (.<=) (lhs:expr) (rhs:expr) = Comparison(lhs, Le, rhs)
let (.>) (lhs:expr) (rhs:expr) = Comparison(lhs, Gt, rhs)
let (.>=) (lhs:expr) (rhs:expr) = Comparison(lhs, Ge, rhs)
let AND lhs rhs = Logical(lhs,And,rhs)
let OR lhs rhs = Logical(lhs,And,rhs)
let IF(condition:expr) = If(condition)
let ELSE = Else
let ENDIF = EndIf
let FOR(var:identifier, from:expr, ``to``:expr) = For(Set(var, from), ``to``, I(1))
let ENDFOR = EndFor
let WHILE(condition) = While(condition)
let ENDWHILE = EndWhile
let SUB(name) = Sub(name)
let ENDSUB = EndSub
let GOSUB(name) = GoSub(name)
let GOTO(label) = Goto(label)
let LABEL(label) = Label(label)

let PRINT x = 
    let writeLine = typeof<System.Console>.GetMethod("WriteLine",[|typeof<obj>|])
    Action(Call(writeLine, [|x|]))

/// Small Basic program
let program =
    [|
        SUB("Modulus")
        "Result" .<- !"Dividend"
        WHILE( !"Result" .>= !"Divisor")
        "Result" .<- !"Result" .- !"Divisor"
        ENDWHILE
        ENDSUB

        FOR("A", I(1), I(100))
        
        "Dividend" .<- !"A"
        "Divisor" .<- I(15)
        GOSUB("Modulus")
        IF(!"Result" .= I(0))
        PRINT(S"FizzBuzz")
        ELSE
        
        "Dividend" .<- !"A"
        "Divisor" .<- I(3)
        GOSUB("Modulus")
        IF(!"Result" .= I(0))
        PRINT(S"Fizz")
        ELSE

        "Dividend" .<- !"A"
        "Divisor" .<- I(5)
        GOSUB("Modulus")
        IF(!"Result" .= I(0))
        PRINT(S"Buzz")
        ELSE

        PRINT(!"A")
        
        ENDIF
        ENDIF
        ENDIF
        ENDFOR
    |]

run program
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type identifier = string

Full name: Script.identifier
type index = int

Full name: Script.index
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type MethodInfo = System.Reflection.MethodInfo

Full name: Script.MethodInfo
namespace System
namespace System.Reflection
type MethodInfo =
  inherit MethodBase
  member Equals : obj:obj -> bool
  member GetBaseDefinition : unit -> MethodInfo
  member GetGenericArguments : unit -> Type[]
  member GetGenericMethodDefinition : unit -> MethodInfo
  member GetHashCode : unit -> int
  member MakeGenericMethod : [<ParamArray>] typeArguments:Type[] -> MethodInfo
  member MemberType : MemberTypes
  member ReturnParameter : ParameterInfo
  member ReturnType : Type
  member ReturnTypeCustomAttributes : ICustomAttributeProvider

Full name: System.Reflection.MethodInfo
type arithmetic =
  | Add
  | Subtract
  | Multiply
  | Divide

Full name: Script.arithmetic


 Small Basic arithmetic operation
union case arithmetic.Add: arithmetic
union case arithmetic.Subtract: arithmetic
union case arithmetic.Multiply: arithmetic
union case arithmetic.Divide: arithmetic
type comparison =
  | Eq
  | Ne
  | Lt
  | Gt
  | Le
  | Ge

Full name: Script.comparison


 Small Basic comparison operaton
union case comparison.Eq: comparison
union case comparison.Ne: comparison
union case comparison.Lt: comparison
union case comparison.Gt: comparison
union case comparison.Le: comparison
union case comparison.Ge: comparison
type logical =
  | And
  | Or

Full name: Script.logical


 Small Basic logical operation
union case logical.And: logical
union case logical.Or: logical
type value =
  | Bool of bool
  | Int of int
  | Double of double
  | String of string

Full name: Script.value


 Small Basic value
union case value.Bool: bool -> value
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
union case value.Int: int -> value
union case value.Double: double -> value
Multiple items
val double : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double

--------------------
type double = System.Double

Full name: Microsoft.FSharp.Core.double
Multiple items
union case value.String: string -> value

--------------------
module String

from Microsoft.FSharp.Core
type expr =
  | Literal of value
  | Var of identifier
  | GetAt of location
  | Func of call
  | Neg of expr
  | Arithmetic of expr * arithmetic * expr
  | Comparison of expr * comparison * expr
  | Logical of expr * logical * expr

Full name: Script.expr


 Small Basic expression
Multiple items
union case expr.Literal: value -> expr

--------------------
type LiteralAttribute =
  inherit Attribute
  new : unit -> LiteralAttribute

Full name: Microsoft.FSharp.Core.LiteralAttribute

--------------------
new : unit -> LiteralAttribute
union case expr.Var: identifier -> expr
union case expr.GetAt: location -> expr
type location = | Location of identifier * expr []

Full name: Script.location
union case expr.Func: call -> expr
type call = | Call of MethodInfo * expr []

Full name: Script.call
union case expr.Neg: expr -> expr
union case expr.Arithmetic: expr * arithmetic * expr -> expr
union case expr.Comparison: expr * comparison * expr -> expr
union case expr.Logical: expr * logical * expr -> expr
union case location.Location: identifier * expr [] -> location
union case call.Call: MethodInfo * expr [] -> call
type assign = | Set of identifier * expr

Full name: Script.assign


 Assignment
Multiple items
union case assign.Set: identifier * expr -> assign

--------------------
module Set

from Microsoft.FSharp.Collections

--------------------
type Set<'T (requires comparison)> =
  interface IComparable
  interface IEnumerable
  interface IEnumerable<'T>
  interface ICollection<'T>
  new : elements:seq<'T> -> Set<'T>
  member Add : value:'T -> Set<'T>
  member Contains : value:'T -> bool
  override Equals : obj -> bool
  member IsProperSubsetOf : otherSet:Set<'T> -> bool
  member IsProperSupersetOf : otherSet:Set<'T> -> bool
  ...

Full name: Microsoft.FSharp.Collections.Set<_>

--------------------
new : elements:seq<'T> -> Set<'T>
type instruction =
  | Assign of assign
  | SetAt of location * expr
  | Action of call
  | For of assign * expr * expr
  | EndFor
  | If of expr
  | Else
  | EndIf
  | While of expr
  | EndWhile
  ...

Full name: Script.instruction


 Small Basic instruction
union case instruction.Assign: assign -> instruction
union case instruction.SetAt: location * expr -> instruction
union case instruction.Action: call -> instruction
union case instruction.For: assign * expr * expr -> instruction
union case instruction.EndFor: instruction
union case instruction.If: expr -> instruction
union case instruction.Else: instruction
union case instruction.EndIf: instruction
union case instruction.While: expr -> instruction
union case instruction.EndWhile: instruction
union case instruction.Sub: identifier -> instruction
union case instruction.EndSub: instruction
union case instruction.GoSub: identifier -> instruction
union case instruction.Label: label -> instruction
type label = string

Full name: Script.label
union case instruction.Goto: label -> instruction
val fromObj : x:obj -> value

Full name: Script.fromObj


 Converts value to obj
val x : obj
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
val x : bool
val x : int
val x : double
val x : string
val raise : exn:System.Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type NotSupportedException =
  inherit SystemException
  new : unit -> NotSupportedException + 2 overloads

Full name: System.NotSupportedException

--------------------
System.NotSupportedException() : unit
System.NotSupportedException(message: string) : unit
System.NotSupportedException(message: string, innerException: exn) : unit
System.Object.ToString() : string
val toObj : _arg1:value -> obj

Full name: Script.toObj


 Converts value to obj
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
val toInt : _arg1:value -> int

Full name: Script.toInt


 Converts value to int
val toBool : _arg1:value -> bool

Full name: Script.toBool


 Converts value to bool
val l : double
val r : double
union case Option.Some: Value: 'T -> Option<'T>
val l : int
val r : int
union case Option.None: Option<'T>
val compare : lhs:value -> rhs:value -> int

Full name: Script.compare


 Compares values
val lhs : value
val rhs : value
val l : bool
val r : bool
active recognizer AsDoubles: value * value -> (double * double) option

Full name: Script.( |AsDoubles|_| )


 Coerces a tuple of numeric values to double
System.Double.CompareTo(value: float) : int
System.Double.CompareTo(value: obj) : int
val l : string
val r : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
namespace System.Collections
namespace System.Collections.Generic
type VarLookup = Dictionary<identifier,value>

Full name: Script.VarLookup
Multiple items
type Dictionary<'TKey,'TValue> =
  new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
  member Add : key:'TKey * value:'TValue -> unit
  member Clear : unit -> unit
  member Comparer : IEqualityComparer<'TKey>
  member ContainsKey : key:'TKey -> bool
  member ContainsValue : value:'TValue -> bool
  member Count : int
  member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Item : 'TKey -> 'TValue with get, set
  ...
  nested type Enumerator
  nested type KeyCollection
  nested type ValueCollection

Full name: System.Collections.Generic.Dictionary<_,_>

--------------------
Dictionary() : unit
Dictionary(capacity: int) : unit
Dictionary(comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : unit
type ArrayLookup = Dictionary<identifier,Dictionary<value,value>>

Full name: Script.ArrayLookup
val eval : VarLookup * ArrayLookup -> expr:expr -> value

Full name: Script.eval


 Evaluates expressions
val state : VarLookup * ArrayLookup
Multiple items
val expr : expr

--------------------
type expr =
  | Literal of value
  | Var of identifier
  | GetAt of location
  | Func of call
  | Neg of expr
  | Arithmetic of expr * arithmetic * expr
  | Comparison of expr * comparison * expr
  | Logical of expr * logical * expr

Full name: Script.expr


 Small Basic expression
val vars : VarLookup
val arrays : ArrayLookup
val x : value
Multiple items
val identifier : identifier

--------------------
type identifier = string

Full name: Script.identifier
Multiple items
val index : expr

--------------------
type index = int

Full name: Script.index
Multiple items
val call : call

--------------------
type call = | Call of MethodInfo * expr []

Full name: Script.call
val invoke : VarLookup * ArrayLookup -> call -> value

Full name: Script.invoke
val x : expr
Multiple items
val arithmetic : lhs:value -> op:arithmetic -> rhs:value -> value

Full name: Script.arithmetic

--------------------
type arithmetic =
  | Add
  | Subtract
  | Multiply
  | Divide

Full name: Script.arithmetic


 Small Basic arithmetic operation
val l : expr
val op : arithmetic
val r : expr
val op : comparison
Multiple items
val comparison : lhs:value -> op:comparison -> rhs:value -> value

Full name: Script.comparison

--------------------
type comparison =
  | Eq
  | Ne
  | Lt
  | Gt
  | Le
  | Ge

Full name: Script.comparison


 Small Basic comparison operaton
val op : logical
Multiple items
val logical : lhs:value -> op:logical -> rhs:value -> value

Full name: Script.logical

--------------------
type logical =
  | And
  | Or

Full name: Script.logical


 Small Basic logical operation
Multiple items
type NotImplementedException =
  inherit SystemException
  new : unit -> NotImplementedException + 2 overloads

Full name: System.NotImplementedException

--------------------
System.NotImplementedException() : unit
System.NotImplementedException(message: string) : unit
System.NotImplementedException(message: string, inner: exn) : unit
val mi : MethodInfo
val args : expr []
val args : obj []
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
System.Reflection.MethodBase.Invoke(obj: obj, parameters: obj []) : obj
System.Reflection.MethodBase.Invoke(obj: obj, invokeAttr: System.Reflection.BindingFlags, binder: System.Reflection.Binder, parameters: obj [], culture: System.Globalization.CultureInfo) : obj
val run : program:instruction [] -> unit

Full name: Script.run


 Runs program
val program : instruction []
val pi : int ref


 Program index
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val variables : Dictionary<identifier,value>


 Variable lookup
val arrays : Dictionary<identifier,Dictionary<value,value>>


 Array lookup
val state : Dictionary<identifier,value> * Dictionary<identifier,Dictionary<value,value>>


 Current state
val forLoops : Dictionary<index,(index * identifier * expr * expr)>


 For from EndFor lookup
val whileLoops : Dictionary<index,index>


 While from EndWhile lookup
val callStack : Stack<index>


 Call stack for Gosubs
Multiple items
type Stack<'T> =
  new : unit -> Stack<'T> + 2 overloads
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member CopyTo : array:'T[] * arrayIndex:int -> unit
  member Count : int
  member GetEnumerator : unit -> Enumerator<'T>
  member Peek : unit -> 'T
  member Pop : unit -> 'T
  member Push : item:'T -> unit
  member ToArray : unit -> 'T[]
  ...
  nested type Enumerator

Full name: System.Collections.Generic.Stack<_>

--------------------
Stack() : unit
Stack(capacity: int) : unit
Stack(collection: IEnumerable<'T>) : unit
val eval : (expr -> value)


 Evaluates expression with variables
Multiple items
val assign : (assign -> unit)


 Assigns result of expression to variable


--------------------
type assign = | Set of identifier * expr

Full name: Script.assign


 Assignment
val findFirstIndex : (int -> (instruction -> bool) * (instruction -> bool) -> instruction list -> int)


 Finds first index of instructions
val start : int
val inc : (instruction -> bool)
val dec : (instruction -> bool)
val instructions : instruction list
val mutable i : int
val mutable nest : int
Multiple items
type List<'T> =
  new : unit -> List<'T> + 2 overloads
  member Add : item:'T -> unit
  member AddRange : collection:IEnumerable<'T> -> unit
  member AsReadOnly : unit -> ReadOnlyCollection<'T>
  member BinarySearch : item:'T -> int + 2 overloads
  member Capacity : int with get, set
  member Clear : unit -> unit
  member Contains : item:'T -> bool
  member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
  member CopyTo : array:'T[] -> unit + 2 overloads
  ...
  nested type Enumerator

Full name: System.Collections.Generic.List<_>

--------------------
List() : unit
List(capacity: int) : unit
List(collection: IEnumerable<'T>) : unit
val exists : predicate:('T -> bool) -> list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.exists
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val findIndex : (int -> (instruction -> bool) * (instruction -> bool) -> instruction -> int)


 Finds index of instruction
Multiple items
val instruction : instruction

--------------------
type instruction =
  | Assign of assign
  | SetAt of location * expr
  | Action of call
  | For of assign * expr * expr
  | EndFor
  | If of expr
  | Else
  | EndIf
  | While of expr
  | EndWhile
  ...

Full name: Script.instruction


 Small Basic instruction
val isIf : (instruction -> bool)
val isEndIf : (instruction -> bool)
val isFor : (instruction -> bool)
val isEndFor : (instruction -> bool)
val isWhile : (instruction -> bool)
val isEndWhile : (instruction -> bool)
val isFalse : ('a -> bool)
val step : (unit -> unit)


 Instruction step
val set : assign
Multiple items
val array : Dictionary<value,value>

--------------------
type 'T array = 'T []

Full name: Microsoft.FSharp.Core.array<_>
Dictionary.TryGetValue(key: identifier, value: byref<Dictionary<value,value>>) : bool
Dictionary.Add(key: identifier, value: Dictionary<value,value>) : unit
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val condition : expr
Multiple items
val index : int

--------------------
type index = int

Full name: Script.index
val from : assign
val target : expr
val step : expr
val start : index
Stack.Push(item: index) : unit
Stack.Pop() : index
Multiple items
val label : label

--------------------
type label = string

Full name: Script.label
property System.Array.Length: int
val incr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.incr
val B : x:bool -> expr

Full name: Script.B
val I : x:int -> expr

Full name: Script.I
val D : x:double -> expr

Full name: Script.D
val S : x:string -> expr

Full name: Script.S
val AT : name:identifier * expr:expr -> location

Full name: Script.AT
val name : identifier
val name : string
Multiple items
val location : location

--------------------
type location = | Location of identifier * expr []

Full name: Script.location
val lhs : expr
val rhs : expr
val AND : lhs:expr -> rhs:expr -> expr

Full name: Script.AND
val OR : lhs:expr -> rhs:expr -> expr

Full name: Script.OR
val IF : condition:expr -> instruction

Full name: Script.IF
val ELSE : instruction

Full name: Script.ELSE
val ENDIF : instruction

Full name: Script.ENDIF
val FOR : var:identifier * from:expr * to:expr -> instruction

Full name: Script.FOR
val var : identifier
val from : expr
val ENDFOR : instruction

Full name: Script.ENDFOR
val WHILE : condition:expr -> instruction

Full name: Script.WHILE
val ENDWHILE : instruction

Full name: Script.ENDWHILE
val SUB : name:identifier -> instruction

Full name: Script.SUB
val ENDSUB : instruction

Full name: Script.ENDSUB
val GOSUB : name:identifier -> instruction

Full name: Script.GOSUB
val GOTO : label:label -> instruction

Full name: Script.GOTO
val LABEL : label:label -> instruction

Full name: Script.LABEL
val PRINT : x:expr -> instruction

Full name: Script.PRINT
val writeLine : System.Reflection.MethodInfo
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
val program : instruction []

Full name: Script.program


 Small Basic program

More information

Link:http://fssnip.net/l7
Posted:3 years ago
Author:Phillip Trelford
Tags: basic , dsl , interpreter , ast