Skip to content

Commit cc31d35

Browse files
committed
os.args and type()
1 parent af02cf9 commit cc31d35

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

lang/src/cli/repl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ impl Highlighter for Syntax {
5454
format!("{}{}", caps[1].blue().to_string(), &caps[2])
5555
}).to_string();
5656

57-
result = self.string_re.replace_all(&result, |caps: &regex::Captures| {
58-
caps[0].green().to_string()
59-
}).to_string();
60-
6157
result = self.comment_re.replace_all(&result, |caps: &regex::Captures| {
6258
caps[0].dimmed().to_string()
6359
}).to_string();
@@ -70,6 +66,10 @@ impl Highlighter for Syntax {
7066
caps[0].cyan().to_string()
7167
}).to_string();
7268

69+
result = self.string_re.replace_all(&result, |caps: &regex::Captures| {
70+
caps[0].green().to_string()
71+
}).to_string();
72+
7373
std::borrow::Cow::Owned(result)
7474
}
7575

lang/src/functions.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn get_functions() -> Vec<BuiltinFn> {
2222
builtin("float", float),
2323
builtin("str", str),
2424
builtin("bool", bool),
25+
builtin("type", r#type),
2526
builtin("exit", exit),
2627
builtin("error", error),
2728
]
@@ -127,6 +128,14 @@ fn bool(args: Vec<Value>) -> Result<Value, String> {
127128
Ok(Value::Bool(args[0].truthy()))
128129
}
129130

131+
fn r#type(args: Vec<Value>) -> Result<Value, String> {
132+
if args.len() != 1 {
133+
return Err(format!("type() takes exactly one argument ({} given)", args.len()));
134+
}
135+
136+
Ok(Value::String(args[0].type_name().to_string()))
137+
}
138+
130139
fn exit(args: Vec<Value>) -> Result<Value, String> {
131140
if args.len() == 0 {
132141
std::process::exit(0);

lang/src/stdlib/os.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub fn object() -> Value {
1414
methods.insert("getenv".to_string(), Value::BuiltinFn(BuiltinFn::new("getenv", getenv)));
1515
methods.insert("setenv".to_string(), Value::BuiltinFn(BuiltinFn::new("setenv", setenv)));
1616
methods.insert("unsetenv".to_string(), Value::BuiltinFn(BuiltinFn::new("unsetenv", unsetenv)));
17+
methods.insert("args".to_string(), Value::BuiltinFn(BuiltinFn::new("args", args)));
18+
1719
methods.insert("name".to_string(), Value::String({
1820
if cfg!(target_os = "windows") {
1921
"windows"
@@ -156,4 +158,9 @@ fn unsetenv(args: Vec<Value>) -> Result<Value, String> {
156158
}
157159

158160
Ok(Value::Null)
161+
}
162+
163+
fn args(_args: Vec<Value>) -> Result<Value, String> {
164+
let args: Vec<Value> = std::env::args().map(|s| Value::String(s)).collect();
165+
Ok(Value::Array(args))
159166
}

lang/src/vm/vm.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ impl VM {
241241
match (func.func)(args) {
242242
Ok(result) => self.stack.push(result),
243243
Err(e) => {
244+
// this isnt great lmfao but it works ig
244245
if func.name == "error" && !e.contains("error() takes exactly one argument") {
245246
return Err(self.runtime_error(format!("{}", e), span));
246247
}

0 commit comments

Comments
 (0)