cao-lang 0.1.67

The back-end of cao-lang, a node based visual scripting language
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use super::Card;
use crate::{subprogram_description, SubProgram, SubProgramType};

#[derive(Debug, Clone, Copy)]
pub enum PropertyName {
    Integer,
    Float,
    Number,
    Value,
    Text,
    Variable,
    Object,
    Boolean,
}

impl PropertyName {
    /// return a list of all available properties
    pub fn all_props() -> &'static [PropertyName] {
        &[
            PropertyName::Integer,
            PropertyName::Float,
            PropertyName::Number,
            PropertyName::Value,
            PropertyName::Text,
            PropertyName::Variable,
            PropertyName::Object,
            PropertyName::Boolean,
        ]
    }

    pub fn to_str(self) -> &'static str {
        match self {
            PropertyName::Integer => "Integer",
            PropertyName::Float => "Float",
            PropertyName::Number => "Number",
            PropertyName::Value => "Value",
            PropertyName::Text => "Text",
            PropertyName::Variable => "Variable",
            PropertyName::Object => "Object",
            PropertyName::Boolean => "Boolean",
        }
    }
}

pub fn get_instruction_descriptions() -> Vec<SubProgram> {
    vec![
        get_desc(Card::Pass),
        get_desc(Card::Add),
        get_desc(Card::Sub),
        get_desc(Card::Mul),
        get_desc(Card::Div),
        get_desc(Card::CopyLast),
        get_desc(Card::Less),
        get_desc(Card::LessOrEq),
        get_desc(Card::Equals),
        get_desc(Card::NotEquals),
        get_desc(Card::Pop),
        get_desc(Card::ClearStack),
        get_desc(Card::And),
        get_desc(Card::Or),
        get_desc(Card::Xor),
        get_desc(Card::Not),
        get_desc(Card::Return),
        get_desc(Card::ScalarNil),
        get_desc(Card::CreateTable),
        get_desc(Card::Abort),
        get_desc(Card::Len),
        get_desc(Card::SetProperty),
        get_desc(Card::GetProperty),
        get_desc(Card::ScalarInt(Default::default())),
        get_desc(Card::ScalarFloat(Default::default())),
        get_desc(Card::StringLiteral(Default::default())),
        get_desc(Card::IfTrue(Default::default())),
        get_desc(Card::IfFalse(Default::default())),
        get_desc(Card::IfElse {
            then: Default::default(),
            r#else: Default::default(),
        }),
        get_desc(Card::Jump(Default::default())),
        get_desc(Card::SetGlobalVar(Default::default())),
        get_desc(Card::SetVar(Default::default())),
        get_desc(Card::ReadVar(Default::default())),
        get_desc(Card::Repeat(Default::default())),
        get_desc(Card::While(Default::default())),
        get_desc(Card::ForEach {
            variable: Default::default(),
            lane: Default::default(),
        }),
    ]
}

fn get_desc(node: Card) -> SubProgram {
    match node {
        Card::CompositeCard { .. } | Card::CallNative(_) => unreachable!(),
        Card::GetProperty => subprogram_description!(
            "GetProperty",
            "Gets a named field in the given table. Returns `nil` if the field does not exist",
            SubProgramType::Object,
            [PropertyName::Object.to_str(), PropertyName::Variable.to_str()],
            [PropertyName::Value.to_str()],
            []
        ),
        Card::SetProperty => subprogram_description!(
            "SetProperty",
            r#"Sets a named field in the given table to the input value
Order of parameters: Table, Property-Key, Value"#,
            SubProgramType::Object,
            [PropertyName::Object.to_str(), PropertyName::Variable.to_str(), PropertyName::Value.to_str()],
            [],
            []
        ),
        Card::CreateTable => subprogram_description!(
            "CreateTable",
            "Initializes a new data table",
            SubProgramType::Object,
            [],
            [],
            []
        ),
        Card::Pass => subprogram_description!(
            "Pass",
            "Do nothing",
            SubProgramType::Instruction,
            [],
            [],
            []
        ),
        Card::Not => subprogram_description!(
            "Not",
            "Logically negates the value on the top of the stack",
            SubProgramType::Instruction,
            [PropertyName::Value.to_str()],
            [PropertyName::Boolean.to_str()],
            []
        ),
        Card::And => subprogram_description!(
            "And",
            "Logical And",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Boolean.to_str()],
            []
        ),
        Card::Or => subprogram_description!(
            "Or",
            "Logical inclusive Or",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Boolean.to_str()],
            []
        ),
        Card::Xor => subprogram_description!(
            "Xor",
            "Logical exclusive Or",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Boolean.to_str()],
            []
        ),
        Card::Add => subprogram_description!(
            "Add",
            "Add two scalars",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::Sub => subprogram_description!(
            "Sub",
            "Subtract two scalars",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::Mul => subprogram_description!(
            "Mul",
            "Multiply two scalars",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::Div => subprogram_description!(
            "Div",
            "Divide two scalars",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::CopyLast => subprogram_description!(
            "CopyLast",
            "Duplicate the last item on the stack",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str()],
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            []
        ),
        Card::Less => subprogram_description!(
            "Less",
            "Return 1 if the first input is less than the second, 0 otherwise",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::LessOrEq => subprogram_description!(
            "LessOrEq",
            "Return 1 if the first input is less than or equal to the second, 0 otherwise",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::Equals => subprogram_description!(
            "Equals",
            "Return 1 if the inputs are equal, 0 otherwise",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::ScalarNil => subprogram_description!(
            "ScalarNil",
            "Push a `Nil` value onto the stack",
            SubProgramType::Instruction,
            [],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::NotEquals => subprogram_description!(
            "NotEquals",
            "Return 0 if the inputs are equal, 1 otherwise",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str(), PropertyName::Number.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::Pop => subprogram_description!(
            "Pop",
            "Pops the top elements on the stack and discards it",
            SubProgramType::Instruction,
            [PropertyName::Number.to_str()],
            [],
            []
        ),

        Card::ClearStack => subprogram_description!(
            "ClearStack",
            "Clears the stack",
            SubProgramType::Instruction,
            [],
            [],
            []
        ),

        Card::Abort => subprogram_description!(
            "Abort",
            "Exit the program",
            SubProgramType::Instruction,
            [],
            [],
            [PropertyName::Integer.to_str()]
        ),

        Card::ScalarInt(_) => subprogram_description!(
            "ScalarInt",
            "Make an integer",
            SubProgramType::Instruction,
            [],
            [PropertyName::Number.to_str()],
            [PropertyName::Integer.to_str()]
        ),

        Card::ScalarFloat(_) => subprogram_description!(
            "ScalarFloat",
            "Make a real number",
            SubProgramType::Instruction,
            [],
            [PropertyName::Number.to_str()],
            [PropertyName::Float.to_str()]
        ),

        Card::StringLiteral(_) => subprogram_description!(
            "StringLiteral",
            "Make a text",
            SubProgramType::Instruction,
            [],
            [PropertyName::Number.to_str()],
            [PropertyName::Text.to_str()]
        ),

        Card::IfTrue(_) => subprogram_description!(
            "IfTrue",
            "Jump to the input lane if the last value is true else do nothing.",
            SubProgramType::Branch,
            [PropertyName::Number.to_str()],
            [],
            [PropertyName::Text.to_str()]
        ),

        Card::IfFalse(_) => subprogram_description!(
            "IfFalse",
            "Jump to the input lane if the last value is false else do nothing.",
            SubProgramType::Branch,
            [PropertyName::Number.to_str()],
            [],
            [PropertyName::Text.to_str()]
        ),

        Card::IfElse { .. } => subprogram_description!(
            "IfElse",
            "Jump to the input lane if the last value is true else jump to the second input lane.",
            SubProgramType::Branch,
            [PropertyName::Number.to_str()],
            [],
            [PropertyName::Text.to_str(), PropertyName::Text.to_str()]
        ),

        Card::Jump(_) => subprogram_description!(
            "Jump",
            "Jump to the input lane.",
            SubProgramType::Branch,
            [],
            [],
            [PropertyName::Text.to_str()]
        ),

        Card::SetGlobalVar(_) => subprogram_description!(
            "SetGlobalVar",
            "Sets the value of a global variable",
            SubProgramType::Instruction,
            [PropertyName::Object.to_str()],
            [],
            [PropertyName::Variable.to_str()]
        ),
        Card::SetVar(_) => subprogram_description!(
            "SetLocalVar",
            "Sets the value of a local variable. Local variables are only usable in the Lane they were created in.",
            SubProgramType::Instruction,
            [PropertyName::Object.to_str()],
            [],
            [PropertyName::Variable.to_str()]
        ),

        Card::ReadVar(_) => subprogram_description!(
            "ReadVar",
            "Read the value of a variable. If the variable does not exist yet it will attempt to read a global variable with the same name",
            SubProgramType::Instruction,
            [],
            [PropertyName::Object.to_str()],
            [PropertyName::Variable.to_str()]
        ),

        Card::Return => subprogram_description!(
            "Return",
            "Return to where this Lane was called",
            SubProgramType::Branch,
            [],
            [],
            []
        ),

        Card::Repeat(_) => subprogram_description!(
            "Repeat",
            "Repeat a lane the input number of times",
            SubProgramType::Branch,
            [PropertyName::Number.to_str()],
            [],
            [PropertyName::Text.to_str()]
        ),

        Card::While(_) => subprogram_description!(
            "While",
            "Repeat a lane until the lane's last value is 0",
            SubProgramType::Branch,
            [],
            [],
            [PropertyName::Text.to_str()]
        ),

        Card::ForEach {..} => subprogram_description!(
            "ForEach",
            "Repeat a lane for each key in the given table, passing the key to the lane as an argument",
            SubProgramType::Branch,
            [],
            [],
            [PropertyName::Text.to_str(), PropertyName::Text.to_str()]
        ),

        Card::Len => subprogram_description!(
            "Len",
            "Pushes the length of a table (number of keys) onto the stack",
            SubProgramType::Branch,
            [PropertyName::Object.to_str()],
            [PropertyName::Number.to_str()],
            []
        ),

        Card::Noop => subprogram_description!(
            "No-op",
            "Does nothing",
            SubProgramType::Instruction,
            [],
            [],
            []
        ),
    }
}