added exmaple program

This commit is contained in:
2025-10-30 19:12:04 +01:00
parent 934d961be8
commit 2069f73715
5 changed files with 72 additions and 9 deletions

View File

@@ -1,3 +1,52 @@
# fudge2
second iteration of the fudge language
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;
]
```

View File

@@ -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<Expression>, Box<Expression>),
Div(Box<Expression>, Box<Expression>),
Mod(Box<Expression>, Box<Expression>),
FuncCall(String, Vec<Expression>),
}
#[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<OptionallyTypedIdentifier>
pub body: Vec<OptionallyTypedIdentifier>,
}
#[derive(Debug, Clone, PartialEq)]

View File

@@ -30,4 +30,5 @@ let "let"
:= "assign"
Type "type"
\@main "main"
[\n;] "linedelim"
[\n;]+ "linedelim"
[\n]+ "newline"

View File

@@ -1,3 +1,4 @@
#![allow(dead_code, unused)]
use crate::errors::CLIArgumentError;
use lrlex::lrlex_mod;
use lrpar::lrpar_mod;

View File

@@ -1,3 +1,4 @@
let a := 1
let b := 2
@main := a
@main := a * b + 3