Compare commits
4 Commits
66f5f3f9a3
...
92027f414d
Author | SHA1 | Date |
---|---|---|
~karx | 92027f414d | |
~karx | 311e4cc1b8 | |
~karx | da5958a125 | |
~karx | f197b1f493 |
|
@ -1,2 +1,2 @@
|
|||
/target
|
||||
inp.txt
|
||||
*.txt
|
||||
|
|
33
README.md
33
README.md
|
@ -30,8 +30,8 @@ The currently available opcodes are as follows:
|
|||
- `p` - print out the arguments: `pHello World!` prints "Hello World!"
|
||||
- `a`, `s`, `m`, `d` - add, subtract, multiply, and divide, respectively: `a2-2` adds 2 + 2.
|
||||
- `l` - declare a variable: `lv9` declares variable `v` with value `9`; doing `p$v` prints out 9.
|
||||
- `f` - declare a function: see [Functions](#functions) for more info
|
||||
- `#` - comment: the interpreter will skip this line.
|
||||
- `f` - declare a function: see [the devlog entry](https:/tilde.team/~karx/blog/sandwich-devlog-3-function-junction.html) for more info
|
||||
- `i` - import variables and functions from another file: see [the devlog entry](https://tilde.team/~karx/blog/from-cup-import-coffee-sandwich-devlog-4.html) for more info.
|
||||
|
||||
Here's an example "Hello world" program:
|
||||
|
||||
|
@ -41,31 +41,6 @@ lwWorld!
|
|||
p$h $w
|
||||
```
|
||||
|
||||
### Functions
|
||||
|
||||
*functions* are ways to "pipe" math output to another operation. For example, you could assign the output to a variable, like below:
|
||||
|
||||
```
|
||||
#Declare a function
|
||||
fxa3-3
|
||||
#Call the function and assign it to variable
|
||||
lv*x
|
||||
#will print out 6
|
||||
p$v
|
||||
```
|
||||
|
||||
Or you could print it out directly:
|
||||
|
||||
```
|
||||
#Declare a function
|
||||
fxs30-3
|
||||
#Call it and print it out
|
||||
#Will print out 27
|
||||
p*x
|
||||
```
|
||||
|
||||
Read [the devlog entry for more](https:/tilde.team/~karx/blog/sandwich-devlog-3-function-junction.html) info.
|
||||
|
||||
## Contributing
|
||||
|
||||
|
||||
|
@ -90,9 +65,9 @@ Read [this guide](https://git-send-email.io) for more information.
|
|||
## TODO
|
||||
|
||||
|
||||
- [ ] Ability to explicitly print math output and assign math output to variables
|
||||
- [x] Ability to explicitly print math output and assign math output to variables
|
||||
- [ ] Better function parsing (multi-instruction functions?)
|
||||
- [ ] Importing/Running other files
|
||||
- [x] Importing/Running other files
|
||||
- [ ] "Verbose" or "Debug" mode (environment variable or flag?)
|
||||
|
||||
## License
|
||||
|
|
15
src/eval.rs
15
src/eval.rs
|
@ -19,3 +19,18 @@ pub fn do_math(arguments: String, operator: char) -> u32 {
|
|||
_ => panic!("SyntaxError: Unknown operator {}", operator),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn args_or_comments(arguments: &str) -> String {
|
||||
let argument_vec: Vec<char> = arguments.chars().collect();
|
||||
let mut builder: String = String::from("");
|
||||
|
||||
for char in argument_vec {
|
||||
if char == '#' {
|
||||
break;
|
||||
} else {
|
||||
builder.push_str(&char.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
builder
|
||||
}
|
39
src/main.rs
39
src/main.rs
|
@ -140,21 +140,38 @@ impl Program {
|
|||
builder
|
||||
}
|
||||
|
||||
fn run_external(&mut self, filename: String) {
|
||||
// Read contents of the provided file and construct a symbolic Program from it
|
||||
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
|
||||
let mut prog = Program::from_string(contents);
|
||||
prog.run();
|
||||
|
||||
for (key, value) in prog.vars.iter() {
|
||||
self.vars.insert(*key, value.to_string());
|
||||
}
|
||||
|
||||
for (key, value) in prog.funcs.iter() {
|
||||
self.funcs.insert(*key, value.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(&mut self, instruction: &String) {
|
||||
// Opcode is the first character, arguments are everything after the first char
|
||||
let opcode = instruction.chars().collect::<Vec<char>>()[0];
|
||||
let arguments = &instruction[1..];
|
||||
let arguments = eval::args_or_comments(&instruction[1..]);
|
||||
|
||||
match opcode {
|
||||
'p' => println!("{}", self.args_or_funcs(&self.args_or_vars(arguments))),
|
||||
'a' => println!("{}", eval::do_math(self.args_or_vars(arguments), '+')),
|
||||
's' => println!("{}", eval::do_math(self.args_or_vars(arguments), '-')),
|
||||
'm' => println!("{}", eval::do_math(self.args_or_vars(arguments), '*')),
|
||||
'd' => println!("{}", eval::do_math(self.args_or_vars(arguments), '/')),
|
||||
'l' => self.add_var(arguments),
|
||||
'f' => self.add_func(arguments),
|
||||
'#' => {} // Do nothing for comments
|
||||
_ => panic!("SyntaxError at opcode {}: Unknown opcode {}", self.pc, opcode),
|
||||
if opcode != '#' {
|
||||
match opcode {
|
||||
'p' => println!("{}", self.args_or_funcs(&self.args_or_vars(&arguments))),
|
||||
'a' => println!("{}", eval::do_math(self.args_or_vars(&arguments), '+')),
|
||||
's' => println!("{}", eval::do_math(self.args_or_vars(&arguments), '-')),
|
||||
'm' => println!("{}", eval::do_math(self.args_or_vars(&arguments), '*')),
|
||||
'd' => println!("{}", eval::do_math(self.args_or_vars(&arguments), '/')),
|
||||
'l' => self.add_var(&arguments),
|
||||
'f' => self.add_func(&arguments),
|
||||
'i' => self.run_external(arguments),
|
||||
_ => panic!("SyntaxError at opcode {}: Unknown opcode {}", self.pc, opcode),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue