implemented method call

This commit is contained in:
Matthieu Jolimaitre 2024-10-25 15:52:47 +02:00
parent d3095d5980
commit d269425943
8 changed files with 352 additions and 253 deletions

37
example/data/list.mc Normal file
View file

@ -0,0 +1,37 @@
// Constructor.
fn List() {
let result = {
data: [],
push: (self, item) => {
push(self.data, item);
},
pop: (self) => {
return pop(self.data);
},
len: (self) => {
return len(self.data);
}
};
return result;
}
// Instantiation.
let list = List();
print("new list", list);
// Pushing 5 items in a loop.
let index = 0;
loop {
if (index >= 5) break;
list.push(index);
index = index + 1;
}
print("pushed list", list); // Misc accesses.
print("length list", list.len()); // Misc accesses.
print("poped item", list.pop()); // Misc accesses.
print("poped list", list); // Misc accesses.
print("list.data", list.data);
print("list[\"data\"]", list["data"]);

View file

@ -1,7 +1,7 @@
#![no_std]
use alloc::string::ToString;
use microlang::Context;
use microlang::eval::Context;
extern crate alloc;

View file

@ -1,6 +1,6 @@
use std::io::{stdin, stdout, Write};
use microlang::Context;
use microlang::eval::Context;
pub fn main() {
let mut context = Context::empty();

View file

@ -1,6 +1,9 @@
use std::{env::args, fs};
use microlang::{Context, Value};
use microlang::{
eval::{Context, Value},
prelude::prelude,
};
pub fn main() {
// Context instantiation.
@ -15,6 +18,9 @@ pub fn main() {
Ok(Value::None)
});
// Import prelude.
prelude(&mut context);
// Script to evaluate.
let lines = fs::read_to_string(args().nth(1).expect("Pass a file as arg 1.")).expect("File could not be read.");