Compare commits

...

4 Commits

Author SHA1 Message Date
~karx 92027f414d Update README
continuous-integration/drone/push Build is passing Details
2021-02-16 19:47:03 +00:00
~karx 311e4cc1b8 Import vars and funcs from external file 2021-02-16 19:14:20 +00:00
~karx da5958a125 Add basic support for running external programs 2021-02-16 18:42:06 +00:00
~karx f197b1f493 Improve comment parser 2021-02-16 18:23:40 +00:00
4 changed files with 48 additions and 41 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
/target
inp.txt
*.txt

View File

@ -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

View File

@ -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
}

View File

@ -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),
}
}
}