diff --git a/README.md b/README.md index a260931..8e3cba1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,52 @@ # fudge2 -second iteration of the fudge language \ No newline at end of file +second iteration of the fudge language + +## example program +```asm + +// Map, Filter, Reduce +sig map := (T => U) => #iterable(T) => #iterable(U) +let map f ls := match ls | (x:xs) => (f x: map f xs) + | [] => [] + +// Side Effects are ugly, C-FFI +sig polluting map := #polluting (Maybe T => U) => Iterable(T) => Nothing +let polluting map f [ls] := + using std/c-ffi/guarantee_no_parallel_map as seqmap +{ + seqmap 0 (len ls) (i => f (ls at i)) +} + +// type traits +sig polluting print := #repr T => Result (Nothing, #error io_error) +let polluting print s := using std/io/write, std/io/stdout { + write stdout (Repr s) +} + +// Overloading? +sig polluting print := Iterable (#repr T) => Result (Nothing, #error io_error) +let polluting print #repr ls := using std\io as io { + map print ls +} + +// Generic types +sig add := T => T => T +let add a b := a + b + +// Partial application +sig inc := T => T +let inc a = add a 1 + +// Evaluate a polluting expression +sig polluting do := #polluting T => Nothing + +@data := struct ( + let arr := Array(0..100) :> inc, + let ls := List(0..100) :> (x=>x*x), + let all := arr ++ ls, +) +@main := do print @data/all; +] + +``` \ No newline at end of file diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 107b09b..5fedacf 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,5 +1,7 @@ -pub mod literal_parsers; +use std::fmt::{Debug, Display}; + pub mod errors; +pub mod literal_parsers; #[derive(Debug, Clone, PartialEq)] pub struct Program { @@ -14,6 +16,16 @@ pub enum Literal { Bool(bool), } +impl Display for Literal { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Literal::Int(i) => write!(f, "i64({})", i), + Literal::Float(fl) => write!(f, "f64({})", fl), + Literal::Bool(b) => write!(f, "bool({})", b), + } + } +} + #[derive(Debug, Clone, PartialEq)] pub enum Expression { Lit(Literal), @@ -23,14 +35,13 @@ pub enum Expression { Mul(Box, Box), Div(Box, Box), Mod(Box, Box), + FuncCall(String, Vec), } + #[derive(Debug, Clone, PartialEq)] pub enum Definition { - Binding { - name: String, - value: Expression, - }, + Binding { name: String, value: Expression }, StructDef(StructDefinition), EnumDef(EnumDefinition), } @@ -75,7 +86,7 @@ pub struct EnumDefinition { #[derive(Debug, Clone, PartialEq)] pub struct EnumdefLiteral { - pub body: Vec + pub body: Vec, } #[derive(Debug, Clone, PartialEq)] diff --git a/src/lexers/fudge.l b/src/lexers/fudge.l index 8b461bc..46ee487 100644 --- a/src/lexers/fudge.l +++ b/src/lexers/fudge.l @@ -30,4 +30,5 @@ let "let" := "assign" Type "type" \@main "main" -[\n;] "linedelim" +[\n;]+ "linedelim" +[\n]+ "newline" diff --git a/src/main.rs b/src/main.rs index e378dff..c1580f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![allow(dead_code, unused)] use crate::errors::CLIArgumentError; use lrlex::lrlex_mod; use lrpar::lrpar_mod; diff --git a/test.txt b/test.txt index 0b18636..29e3134 100644 --- a/test.txt +++ b/test.txt @@ -1,3 +1,4 @@ let a := 1 +let b := 2 -@main := a \ No newline at end of file +@main := a * b + 3 \ No newline at end of file