teko/
data_structures.rs

1//! Data structures used by the Teko library
2
3use std::collections::HashMap;
4use std::rc::Rc;
5
6use num::BigInt;
7
8/// Evaluation commands used internally by the interpreter
9///
10/// When put on the stack these values have different effects on the interpreter.
11#[derive(Debug, PartialEq)]
12pub enum Commands {
13	Call(Statement),
14	Prepare(Statement),
15	Parameterize,
16	Deparameterize(Vec<String>),
17	If(Statement, Statement),
18	Wind,
19	Evaluate,
20	Empty,
21}
22
23/// Top level data structure used by the parser and interpreter
24#[derive(Debug)]
25pub struct Sourcedata(pub Option<Source>, pub Coredata);
26/// Top level statements are reference counted `Sourcedata`
27pub type Statement = Rc<Sourcedata>;
28/// A program is an ordered sequence of `Statement`
29pub type Program = Vec<Statement>;
30
31/// Denotes a "transfer function" that transform the state of the program
32///
33pub type Transfer = fn(program: &mut Program, env: &mut Env) -> Option<String>;
34/// Boolean values
35#[derive(Debug, PartialEq)]
36pub enum Boolean {
37	True,
38	False,
39}
40
41/// Function types that can be called by the interpreter
42pub enum Function {
43	/// A function written in the implementation language
44	Builtin(Transfer, String),
45	/// Parameter names with a sequence of statements that are inserted into the program when called
46	Library(Vec<String>, Program),
47}
48
49/// Macro types that can be called by the interpreter
50pub enum Macro {
51	/// A function written in the implementation language
52	Builtin(Transfer, String),
53	/// Parameter name with a sequence of statements that are inserted into the program when called
54	Library(String, Program),
55}
56
57/// Core data types of the Teko machine
58#[derive(Debug)]
59pub enum Coredata {
60	/// Denote true and false
61	Boolean(Boolean),
62	/// Error type
63	Error(Statement),
64	/// Function type
65	Function(Function),
66	/// Integer numbers
67	Integer(BigInt),
68	/// Internal commands (used by the implementation)
69	Internal(Commands),
70	/// Macro types
71	Macro(Macro),
72	/// Null (an empty list)
73	Null,
74	/// A pair of data items
75	Pair(Rc<Sourcedata>, Rc<Sourcedata>),
76	/// String type
77	String(String),
78	/// Symbol type
79	Symbol(String),
80}
81
82/// Environment used by the implementation
83pub struct Env {
84	/// Maps variables to stacks of variables (Program)
85	pub store: HashMap<String, Program>,
86	/// Parameter stack used for function calls
87	pub params: Vec<Program>,
88	/// Register used to store results of previous computations
89	pub result: Statement,
90}
91
92/// State used by the parser internally
93#[derive(Clone, Debug)]
94pub struct ParseState {
95	/// Most recent position in the stream being read
96	pub current_read_position: Source,
97	/// Last position where the beginning of a lexeme was initiated
98	pub start_of_current_lexeme: Source,
99	/// Stack of yet unmatched opening parentheses
100	pub unmatched_opening_parentheses: Vec<Source>,
101	/// The current lexeme being built into a token
102	pub token: String,
103	/// The output program
104	pub stack: Program,
105	/// Error container, set to Some if the parser fails
106	pub error: Option<String>,
107}
108
109/// Information about the source of data.
110#[derive(Clone, Debug)]
111pub struct Source {
112	/// Line number of the input, starts at 1
113	pub line: usize,
114	/// Column number of the input, starts at 1
115	pub column: usize,
116	/// Free-form string describing the source
117	pub source: String,
118}