full brainfuck

This commit is contained in:
2022-02-24 16:11:58 +01:00
parent cb51d88726
commit 22c3c2741a
3 changed files with 127 additions and 12 deletions

View File

@@ -3,9 +3,14 @@
<assign variable="prog"> <assign variable="prog">
<call function="string-split"> <call function="string-split">
<arguments> <arguments>
<call function="input"> <call function="array-get">
<arguments>
<call function="get-args">
<arguments /> <arguments />
</call> </call>
<integer value="1" />
</arguments>
</call>
<string value="" /> <string value="" />
</arguments> </arguments>
</call> </call>
@@ -19,9 +24,47 @@
<assign variable="t"> <assign variable="t">
<array /> <array />
</assign> </assign>
<assign variable="found">
<integer value="0" />
</assign>
<assign variable="inp">
<integer value="0" />
</assign>
<each variable="c">
<value variable="prog" />
<do>
<if>
<and>
<equal>
<value variable="found" />
<integer value="0" />
</equal>
<equal>
<value variable="c" />
<string value="," />
</equal>
</and>
<then>
<assign variable="in">
<call function="string-split">
<arguments>
<call function="input">
<arguments />
</call>
<string value="" />
</arguments>
</call>
</assign>
<assign variable="found">
<integer value="1" />
</assign>
</then>
</if>
</do>
</each>
<for variable="_"> <for variable="_">
<from><integer value="0" /></from> <from><integer value="0" /></from>
<to><integer value="100" /></to> <to><integer value="10" /></to>
<step><integer value="1" /></step> <step><integer value="1" /></step>
<do> <do>
<call function="array-push"> <call function="array-push">
@@ -133,6 +176,8 @@
</equal> </equal>
<then> <then>
<call function="print"> <call function="print">
<arguments>
<call function="to-ascii">
<arguments> <arguments>
<call function="array-get"> <call function="array-get">
<arguments> <arguments>
@@ -142,6 +187,8 @@
</call> </call>
</arguments> </arguments>
</call> </call>
</arguments>
</call>
</then> </then>
<else> <else>
<if> <if>
@@ -150,7 +197,28 @@
<value variable="c" /> <value variable="c" />
</equal> </equal>
<then> <then>
<call function="array-set">
<arguments>
<value variable="t" />
<value variable="p" />
<call function="from-ascii">
<arguments>
<call function="array-get">
<arguments>
<value variable="in" />
<value variable="inp" />
</arguments>
</call>
</arguments>
</call>
</arguments>
</call>
<assign variable="inp">
<add>
<value variable="inp" />
<integer value="1" />
</add>
</assign>
</then> </then>
<else> <else>
<if> <if>
@@ -337,11 +405,7 @@
</while> </while>
</then> </then>
</if> </if>
</then> </then>
<else>
</else>
</if> </if>
</else> </else>
</if> </if>
@@ -363,6 +427,11 @@
<integer value="1" /> <integer value="1" />
</add> </add>
</assign> </assign>
<!-- <call function="print">
<arguments>
<value variable="t" />
</arguments>
</call> -->
</do> </do>
</while> </while>
</main> </main>

View File

@@ -832,6 +832,10 @@ mod stl {
String::from("array-length"), String::from("array-length"),
Value::StdFunction(array_length), Value::StdFunction(array_length),
); );
ctx.assign(String::from("to-ascii"), Value::StdFunction(to_ascii));
ctx.assign(String::from("from-ascii"), Value::StdFunction(from_ascii));
ctx.assign(String::from("get-args"), Value::StdFunction(get_args));
} }
fn print(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> { fn print(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
@@ -964,6 +968,48 @@ mod stl {
Err("bad argument count in call to 'array-length'")? Err("bad argument count in call to 'array-length'")?
} }
} }
fn to_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
if vals.len() == 1 {
if let Value::Integer(i) = &vals[0] {
if i <= &255 {
Ok(Some(Value::String(String::from_utf8(vec![*i as u8])?)))
} else {
Err("invalid integer in call to 'to-ascii'")?
}
} else {
Err("invalid argument in call to 'to-ascii'")?
}
} else {
Err("bad argument count in call to 'to-ascii'")?
}
}
fn from_ascii(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
if vals.len() == 1 {
if let Value::String(s) = &vals[0] {
if s.len() == 1 {
Ok(Some(Value::Integer(s.as_bytes()[0] as i64)))
} else {
Err("invalid string in call to 'from-ascii'")?
}
} else {
Err("invalid argument in call to 'from-ascii'")?
}
} else {
Err("bad argument count in call to 'from-ascii'")?
}
}
fn get_args(vals: Vec<Value>) -> Result<Option<Value>, Box<dyn Error>> {
if vals.len() == 0 {
Ok(Some(Value::Array(Rc::new(RefCell::new(
std::env::args().skip(1).map(|arg| Value::String(arg)).collect()
)))))
} else {
Err("bad argument count in call to 'get-args'")?
}
}
} }
pub fn run(filename: &str) -> Result<(), Box<dyn Error>> { pub fn run(filename: &str) -> Result<(), Box<dyn Error>> {

View File

@@ -4,6 +4,6 @@ use std::env;
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if let Err(e) = run(&args[1]) { if let Err(e) = run(&args[1]) {
panic!("Error occurred: {}", e); eprintln!("Error occurred: {}", e);
} }
} }