This commit is contained in:
me 2025-12-26 18:38:55 +02:00
parent 7258fe035a
commit 6c0b63b95c
7 changed files with 108 additions and 23 deletions

View file

@ -1,16 +1,5 @@
let map = fn(f,array) {
let mut index = 0
loop {
if index >= array.len {
break
}
array.index = f(array.index)
index = index + 1
}
array
}
include "./stdlib.ayin"
let main = fn() {
map(fn (x) { x + 1 }, [1,2,3])
}

31
programs/stdlib.ayin Normal file
View file

@ -0,0 +1,31 @@
let map = fn(f,array) {
let mut index = 0
loop {
if index >= array.len {
break
}
array.index = f(array.index)
index = index + 1
}
array
}
let min = fn(a,b) {
if a < b {
a
} else {
b
}
}
let max = fn(a,b) {
if a < b {
b
} else {
a
}
}
let abs = fn(a) {
if a >= 0 { a } else { 0 - a }
}

View file

@ -5,10 +5,11 @@ use std::collections::BTreeMap;
/// A Program.
#[derive(PartialEq, PartialOrd, Debug, Clone)]
pub struct Program {
pub includes: Vec<File>,
pub defs: Vec<Definition>,
}
#[derive(PartialEq, PartialOrd, Debug, Clone)]
#[derive(PartialEq, Eq, Hash, PartialOrd, Debug, Clone)]
pub struct File(pub String);
#[derive(PartialEq, PartialOrd, Debug, Clone)]
@ -230,7 +231,10 @@ impl From<bool> for Expr {
impl From<Vec<Definition>> for Program {
fn from(defs: Vec<Definition>) -> Program {
Program { defs }
Program {
defs,
includes: vec![],
}
}
}

View file

@ -1,15 +1,12 @@
fn main() {
match read_file() {
let args: Vec<String> = std::env::args().collect();
let file = args[1].clone();
match ayin::parser::parse_program(&file) {
Err(err) => println!("Error: {err:#?}"),
Ok(txt) => match ayin::run_main(&txt) {
Ok(program) => match ayin::interpret::run(program, "main".into(), vec![]) {
Err(err) => println!("Error: {err:#?}"),
Ok(e) => println!("{e:#?}"),
},
}
}
fn read_file() -> std::io::Result<String> {
let args: Vec<String> = std::env::args().collect();
let file = args[1].clone();
std::fs::read_to_string(file)
}

View file

@ -3,7 +3,69 @@ pub mod parser;
pub mod scanner;
pub mod types;
use std::collections::HashMap;
pub fn parse_file(code: String) -> Result<crate::ast::Program, types::Error> {
let mut tokens = scanner::scan(code);
parser::parse(&mut tokens, parser::parse_program)
}
pub fn parse_program(file: &String) -> Result<crate::ast::Program, Error> {
let file = std::path::Path::new(&file).canonicalize().unwrap();
let file = crate::ast::File(file.to_str().unwrap().into());
let mut programs: HashMap<crate::ast::File, crate::ast::Program> = HashMap::new();
parse_files(&file, &mut programs)?;
let mut defs: Vec<crate::ast::Definition> = vec![];
for (_, program) in programs.iter_mut() {
defs.append(&mut program.defs);
}
Ok(crate::ast::Program {
defs,
includes: vec![],
})
}
fn parse_files(
file: &crate::ast::File,
programs: &mut HashMap<crate::ast::File, crate::ast::Program>,
) -> Result<(), Error> {
if programs.get(file).is_some() {
return Ok(());
}
match std::fs::read_to_string(&file.0) {
Err(err) => Err(Error::IO(err)),
Ok(code) => match parse_file(code) {
Err(err) => Err(Error::Parser(err)),
Ok(program) => {
let includes = program.includes.clone();
programs.insert(file.clone(), program);
for include in includes {
let p = get_dir(file);
let parent = std::path::Path::new(&p);
let file = std::path::Path::new(&include.0);
let file = parent.join(file);
let file = file.to_str().unwrap().to_string();
parse_files(&crate::ast::File(file), programs)?;
}
Ok(())
}
},
}
}
fn get_dir(file: &crate::ast::File) -> String {
std::path::Path::new(&file.0)
.canonicalize()
.unwrap()
.parent()
.unwrap()
.to_str()
.unwrap()
.to_string()
}
#[derive(Debug)]
pub enum Error {
Parser(types::Error),
IO(std::io::Error),
}

View file

@ -15,7 +15,8 @@ pub fn parse<T>(tokens: &mut Tokens, parser: Parser<T>) -> Result<T, Error> {
}
pub fn parse_program(tokens: &mut Tokens) -> ParseResult<ast::Program> {
Ok(parse_definitions(tokens)?.map(|defs| ast::Program { defs }))
let includes = parse_includes(tokens)?.unwrap_or(vec![]);
Ok(parse_definitions(tokens)?.map(|defs| ast::Program { defs, includes }))
}
fn parse_includes(tokens: &mut Tokens) -> ParseResult<Vec<ast::File>> {

View file

@ -4,6 +4,7 @@ expression: result
---
Ok(
Program {
includes: [],
defs: [
Definition {
mutable: false,