steal from jai

This commit is contained in:
2026-06-01 22:40:24 -04:00
parent d864adfd05
commit 1d568f8ce3
17 changed files with 231 additions and 84 deletions
+15 -4
View File
@@ -1,3 +1,5 @@
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy)]
pub struct Span {
pub file: usize,
@@ -32,7 +34,7 @@ pub struct CompilerMsg {
#[derive(Default)]
pub struct CompilerOutput {
pub errors: Vec<CompilerMsg>,
pub files: Vec<String>,
pub files: Vec<PathBuf>,
}
impl CompilerOutput {
@@ -98,7 +100,7 @@ impl Span {
&text[self.start..=srange.end - 1],
)?;
if *eline != *sline + 1 {
writeln!(w, " ...")?;
writeln!(w, " ...")?;
}
writeln!(
w,
@@ -112,10 +114,19 @@ impl Span {
}
impl From<String> for CompilerMsg {
fn from(value: String) -> Self {
fn from(msg: String) -> Self {
Self {
spans: Vec::new(),
msg: value.to_string(),
msg,
}
}
}
impl<S: Into<String>> From<(S, Span)> for CompilerMsg {
fn from((msg, span): (S, Span)) -> Self {
Self {
spans: vec![span],
msg: msg.into(),
}
}
}
+3
View File
@@ -2,3 +2,6 @@ mod namespace;
pub use namespace::*;
use super::Id;
pub struct Fn {
}
+1 -7
View File
@@ -1,17 +1,11 @@
use super::*;
use crate::parser::Ident;
use std::collections::HashMap;
#[derive(Default)]
pub struct Namespace {
pub items: HashMap<Ident, Item>,
pub items: HashMap<String, Item>,
}
pub enum Item {
Import(Id<Namespace>),
}
// issue: if I try to parse a function body, I'll want to have clear statements such as
// "call trait fn func on x" or "call field func of x", but you (often) can't tell until typed
// x.func
// x'func
+2 -9
View File
@@ -1,8 +1,4 @@
use crate::{
io::CompilerOutput,
parser::{Node, parse_file},
parser_ir::parse_program,
};
use crate::{io::CompilerOutput, parser_ir::parse_program};
mod io;
mod ir;
@@ -16,9 +12,6 @@ fn main() {
return;
};
let mut output = CompilerOutput::new();
let root = parse_file(&path, &mut output);
if let Some(root) = root {
print!("{}", root.new_dsp());
}
let ir = parse_program(&path, &mut output);
output.write(&mut std::io::stdout());
}
+3 -3
View File
@@ -64,12 +64,12 @@ impl<'a> Cursor<'a> {
if next == *token {
Ok(next)
} else {
self.unexpected(&next, &format!("'{token}'"))
self.unexpected(next, &format!("'{token}'"))
}
}
pub fn unexpected<T>(&self, token: &Token, expected: &str) -> Result<T, CompilerMsg> {
Err(CompilerMsg::unexpected_token(token, self.span, expected))
pub fn unexpected<T>(&self, token: Token, expected: &str) -> Result<T, CompilerMsg> {
Err(CompilerMsg::unexpected_token(&token, self.span, expected))
}
pub fn peek_start(&mut self) -> usize {
+2 -5
View File
@@ -26,7 +26,6 @@ def_tokens! {
DashEqual: "-=",
AsteriskEqual: "*=",
SlashEqual: "/=",
DoubleColon: "::",
Hash: "#",
}
keyword {
@@ -38,6 +37,7 @@ def_tokens! {
While: "while",
For: "for",
Match: "match",
Break: "break",
}
other {
Ident(String),
@@ -112,10 +112,7 @@ impl Iterator for Tokens<'_> {
_ => Token::Slash,
'=' => Token::SlashEqual,
},
':' => then! {
_ => Token::Colon,
':' => Token::DoubleColon,
},
':' => Token::Colon,
';' => Token::Semicolon,
'=' => then! {
_ => Token::Equal,
+7 -4
View File
@@ -2,22 +2,25 @@ mod cursor;
mod node;
mod nodes;
use std::path::Path;
use cursor::*;
pub use node::*;
pub use nodes::*;
use crate::io::CompilerOutput;
pub fn parse_file(path: &str, output: &mut CompilerOutput) -> Option<Body> {
let code = match std::fs::read_to_string(path) {
pub fn parse_file(path: impl AsRef<Path>, output: &mut CompilerOutput) -> Option<Body> {
let code = match std::fs::read_to_string(&path) {
Ok(code) => code,
Err(err) => {
output.error(format!("Failed to read input file: {err}"));
return None;
}
};
output.files.push(path.to_string());
let mut ctx = ParseCtx::new(Cursor::new(&code, 0));
let file = output.files.len();
output.files.push(path.as_ref().to_path_buf());
let mut ctx = ParseCtx::new(Cursor::new(&code, file));
let root = match ctx.parse() {
Ok(v) => v,
Err(msg) => {
+1 -1
View File
@@ -30,7 +30,7 @@ impl<'a> ParseCtx<'a> {
) -> Result<N, CompilerMsg> {
let old_start = self.start;
self.start = self.cursor.peek_start();
let res = f(self).map(|r| r);
let res = f(self);
self.start = old_start;
res
}
+4 -4
View File
@@ -1,7 +1,7 @@
use super::*;
pub struct Body {
pub items: Vec<Item>,
pub items: Vec<Expr>,
pub final_semicolon: bool,
pub span: Span,
}
@@ -16,9 +16,9 @@ impl Node for Body {
if at_end(ctx) {
break true;
}
let item: Item = ctx.parse()?;
let needs_semicolon = item.needs_semicolon();
items.push(item);
let expr: Expr = ctx.parse()?;
let needs_semicolon = expr.needs_semicolon();
items.push(expr);
if at_end(ctx) {
break false;
}
+92 -8
View File
@@ -3,22 +3,48 @@ use crate::parser::VecDspT;
pub use super::*;
pub struct Expr {
span: Span,
ty: ExprTy,
pub span: Span,
pub ty: ExprTy,
}
pub enum ExprTy {
Block(Body),
Group(Box<Expr>),
Member {
of: Box<Expr>,
field: Ident,
},
Ident(Ident),
Lit(Lit),
Negate(Box<Expr>),
Call { target: Box<Expr>, args: Vec<Expr> },
Assign { target: Box<Expr>, val: Box<Expr> },
If { cond: Box<Expr>, body: Box<Expr> },
Loop { body: Box<Expr> },
While { cond: Box<Expr>, body: Box<Expr> },
Call {
target: Box<Expr>,
args: Vec<Expr>,
},
Assign {
target: Box<Expr>,
val: Box<Expr>,
},
Define {
target: Box<Expr>,
ty: Option<Type>,
const_: bool,
val: Box<Expr>,
},
If {
cond: Box<Expr>,
body: Box<Expr>,
},
Loop {
body: Box<Expr>,
},
While {
cond: Box<Expr>,
body: Box<Expr>,
},
Import(Ident),
Fn(Box<Func>),
Break,
}
impl Node for Expr {
@@ -32,12 +58,39 @@ impl Node for Expr {
let val = Box::new(ctx.parse_with(Self::unit)?);
ExprTy::Assign { target, val }
}
Token::Colon => {
ctx.next();
let target = Box::new(res);
let mut ty = None;
let next = ctx.expect_peek()?;
if !matches!(next, Token::Equal | Token::Colon) {
ty = Some(ctx.parse()?);
}
let const_ = match ctx.expect_next()? {
Token::Equal => false,
Token::Colon => true,
t => ctx.unexpected(t, "an equals = or colon :")?,
};
let val = Box::new(ctx.parse_with(Self::unit)?);
ExprTy::Define {
target,
ty,
val,
const_,
}
}
Token::OpenParen => {
ctx.next();
let target = Box::new(res);
let args = ctx.list(Token::Comma, Token::CloseParen)?;
ExprTy::Call { target, args }
}
Token::Dot => {
ctx.next();
let of = Box::new(res);
let field = ctx.parse()?;
ExprTy::Member { of, field }
}
_ => break,
};
res = Self {
@@ -69,6 +122,21 @@ impl ExprTy {
Self::Assign { target, val } => {
write!(f, "{} = {}", target.dsp(ctx), val.dsp(ctx))
}
Self::Define {
target,
ty,
val,
const_,
} => {
write!(f, "{} :", target.dsp(ctx))?;
if let Some(ty) = ty {
write!(f, " {} ", ty.dsp(ctx))?;
}
write!(f, "{} {}", if *const_ { ":" } else { "=" }, val.dsp(ctx))
}
Self::Member { of, field } => {
write!(f, "{}.{field}", of.dsp(ctx))
}
Self::If { cond, body } => {
write!(f, "if {} {}", cond.dsp(ctx), body.dsp(ctx))
}
@@ -88,6 +156,12 @@ impl ExprTy {
write!(f, "}}")?;
Ok(())
}
Self::Import(ident) => {
write!(f, "import {ident}")
}
Self::Break => {
write!(f, "break")
}
}
}
}
@@ -137,7 +211,12 @@ impl Expr {
ctx.expect(Token::CloseCurly)?;
ExprTy::Block(body)
}
other => return ctx.unexpected(&other, "an expression"),
Token::Break => ExprTy::Break,
Token::Import => {
let ident = ctx.parse()?;
ExprTy::Import(ident)
}
other => return ctx.unexpected(other, "an expression"),
};
Ok(Self {
ty,
@@ -179,8 +258,13 @@ impl Expr {
| ExprTy::If { body, .. }
| ExprTy::Negate(body)
| ExprTy::Assign { val: body, .. } => body.ends_with_block(),
| ExprTy::Define { val: body, .. } => body.ends_with_block(),
ExprTy::Fn(f) => f.ends_with_block(),
_ => false,
}
}
pub fn needs_semicolon(&self) -> bool {
!self.ends_with_block()
}
}
-13
View File
@@ -2,7 +2,6 @@ use super::*;
pub struct Func {
args: Vec<Param>,
name: Option<Ident>,
ret: Option<Type>,
body: Expr,
span: Span,
@@ -10,14 +9,6 @@ pub struct Func {
impl Node for Func {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
let mut name = None;
if let Token::Ident(ident) = ctx.expect_peek()? {
// yucky
let ident = ident.to_string();
ctx.next();
let ident = ctx.ident(ident);
name = Some(ident);
}
ctx.expect(Token::OpenParen)?;
let args = ctx.list(Token::Comma, Token::CloseParen)?;
let mut ret = None;
@@ -29,16 +20,12 @@ impl Node for Func {
args,
ret,
body,
name,
span: ctx.span(),
})
}
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result {
write!(f, "fn")?;
if let Some(name) = &self.name {
write!(f, " {name}")?;
}
write!(f, "(")?;
if let Some((last, rest)) = self.args.split_last() {
for arg in rest {
+1 -1
View File
@@ -9,7 +9,7 @@ impl Node for Ident {
fn parse(ctx: &mut super::ParseCtx) -> Result<Self, crate::io::CompilerMsg> {
match ctx.expect_next()? {
Token::Ident(ident) => Ok(ctx.ident(ident)),
t => ctx.unexpected(&t, "an identifier"),
t => ctx.unexpected(t, "an identifier"),
}
}
-2
View File
@@ -2,7 +2,6 @@ mod body;
mod expr;
mod func;
mod ident;
mod item;
mod param;
mod struct_;
mod ty;
@@ -10,7 +9,6 @@ pub use body::*;
pub use expr::*;
pub use func::*;
pub use ident::*;
pub use item::*;
pub use param::*;
pub use ty::*;
+1 -1
View File
@@ -8,7 +8,7 @@ impl Node for Type {
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg> {
Ok(match ctx.expect_next()? {
Token::Ident(s) => Self::Ident(ctx.ident(s)),
t => ctx.unexpected(&t, "a type")?,
t => ctx.unexpected(t, "a type")?,
})
}
+85 -13
View File
@@ -1,23 +1,95 @@
use crate::{
io::CompilerOutput,
ir::{Ir, Namespace},
parser::{self, parse_file},
use std::{
collections::{HashMap, HashSet},
path::Path,
};
pub fn parse_program(path: &str, output: &mut CompilerOutput) -> Option<Ir> {
let root = parse_file(path, output)?;
let mut ir = Ir::default();
add_defs(ir.root(), &root);
use crate::{
io::{CompilerMsg, CompilerOutput, Span},
ir::Ir,
parser::{self, ExprTy, Ident, Node, parse_file},
};
pub fn parse_program(path: impl AsRef<Path>, output: &mut CompilerOutput) -> Option<Ir> {
let path = path.as_ref();
let mut imports = Imports::default();
let dir = path.parent().unwrap();
imports.add(path.file_stem().unwrap().to_str().unwrap());
while let Some(next) = imports.new.pop() {
imports.done.insert(next.clone());
let path = dir.join(next + ".lang");
println!("=== {path:?}");
let root = parse_file(path, output)?;
print!("{}", root.new_dsp());
let defs = scan(&mut imports, &root, output);
for (name, spans) in &defs.duplicates {
output.error(CompilerMsg {
msg: format!("Multiple definitions found for {name}"),
spans: spans.clone(),
});
}
}
if !output.errors.is_empty() {
return None;
}
let ir = Ir::default();
Some(ir)
}
pub fn add_defs(namespace: &mut Namespace, body: &parser::Body) {
pub fn scan(imports: &mut Imports, body: &parser::Body, output: &mut CompilerOutput) -> Defs {
let mut defs = Defs::default();
for item in &body.items {
match &item.ty {
parser::ItemTy::Let { name, ty, val } => todo!(),
parser::ItemTy::Fn(func) => todo!(),
parser::ItemTy::Expr(expr) => todo!(),
parser::ItemTy::Import(ident) => todo!(),
ExprTy::Define { target, const_, .. } if *const_ => match &target.ty {
ExprTy::Ident(name) => defs.add(name),
_ => output.error(("Invalid left hand side of definition", target.span)),
},
ExprTy::Import(import) => {
defs.add(import);
imports.add(&import.name);
}
_ => (),
}
}
defs
}
#[derive(Default)]
pub struct Defs {
map: HashMap<String, (usize, Span)>,
duplicates: HashMap<String, Vec<Span>>,
next_id: usize,
}
impl Defs {
pub fn add(&mut self, ident: &Ident) {
if let Some(def) = self.map.get(&ident.name) {
if let Some(spans) = self.duplicates.get_mut(&ident.name) {
spans.push(ident.span);
} else {
self.duplicates
.insert(ident.name.clone(), vec![def.1, ident.span]);
}
return;
}
self.map
.insert(ident.name.clone(), (self.next_id, ident.span));
self.next_id += 1;
}
}
#[derive(Default)]
pub struct Imports {
done: HashSet<String>,
new: Vec<String>,
}
impl Imports {
pub fn add(&mut self, name: &str) {
if self.done.contains(name) || self.new.iter().any(|v| v == name) {
return;
}
self.new.push(name.to_string());
}
}
+11 -8
View File
@@ -1,14 +1,17 @@
modl other;
let x: i32 = 3;
x : i32 = 3;
while true {
print("hello");
print(x);
};
print("hello");
print(x);
other.thing();
thing();
break;
}
let y = true;
y :: true;
if y => print("hello");
fn thing() {
thing :: fn() {
}
import other;
+3 -1
View File
@@ -1,3 +1,5 @@
fn thing() {
thing :: fn() {
print("hello from other");
}
import main;