BRANCHING (TURING COMPLETE????)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use super::{Node, PIdent, Parsable, ParseResult, ParserCtx, Symbol, CompilerMsg};
|
||||
use super::{CompilerMsg, Node, PIdent, Parsable, ParseResult, ParserCtx, Symbol};
|
||||
|
||||
pub struct PInstruction {
|
||||
pub op: Node<PIdent>,
|
||||
@@ -36,7 +36,19 @@ impl Parsable for PAsmArg {
|
||||
return ParseResult::Ok(Self::Value(ident));
|
||||
}
|
||||
|
||||
let next = ctx.expect_peek()?;
|
||||
let mut next = ctx.expect_peek()?;
|
||||
if next.is_symbol(Symbol::Minus) {
|
||||
ctx.next();
|
||||
if let Some(mut ident) = ctx.maybe_parse::<PIdent>() {
|
||||
// TODO: this is so messed up
|
||||
if let Some(i) = ident.as_mut() {
|
||||
i.0.insert(0, '-')
|
||||
}
|
||||
return ParseResult::Ok(Self::Value(ident));
|
||||
}
|
||||
next = ctx.expect_peek()?;
|
||||
}
|
||||
|
||||
if !next.is_symbol(Symbol::OpenCurly) {
|
||||
return ParseResult::Err(CompilerMsg::unexpected_token(
|
||||
next,
|
||||
|
||||
@@ -19,6 +19,9 @@ pub enum PExpr {
|
||||
Group(BoxNode),
|
||||
AsmBlock(Node<PAsmBlock>),
|
||||
Construct(Node<PConstruct>),
|
||||
If(BoxNode, BoxNode),
|
||||
Loop(BoxNode),
|
||||
Break,
|
||||
}
|
||||
|
||||
impl Parsable for PExpr {
|
||||
@@ -42,6 +45,18 @@ impl Parsable for PExpr {
|
||||
Self::Group(res.node.bx())
|
||||
} else if next.is_symbol(Symbol::OpenCurly) {
|
||||
Self::Block(PBlock::parse_node(ctx)?)
|
||||
} else if next.is_keyword(Keyword::If) {
|
||||
ctx.next();
|
||||
let cond = ctx.parse()?.bx();
|
||||
let body = ctx.parse()?.bx();
|
||||
Self::If(cond, body)
|
||||
} else if next.is_keyword(Keyword::Loop) {
|
||||
ctx.next();
|
||||
let body = ctx.parse()?.bx();
|
||||
Self::Loop(body)
|
||||
} else if next.is_keyword(Keyword::Break) {
|
||||
ctx.next();
|
||||
Self::Break
|
||||
} else if next.is_keyword(Keyword::Asm) {
|
||||
ctx.next();
|
||||
Self::AsmBlock(ctx.parse()?)
|
||||
@@ -143,14 +158,13 @@ impl Debug for PExpr {
|
||||
}
|
||||
f.write_char(')')?;
|
||||
}
|
||||
PExpr::UnaryOp(op, e) => {
|
||||
write!(f, "(")?;
|
||||
write!(f, "{}", op.str())?;
|
||||
write!(f, "{:?})", *e)?;
|
||||
}
|
||||
PExpr::UnaryOp(op, e) => write!(f, "({}{:?})", op.str(), e)?,
|
||||
PExpr::Group(inner) => inner.fmt(f)?,
|
||||
PExpr::AsmBlock(inner) => inner.fmt(f)?,
|
||||
PExpr::Construct(inner) => inner.fmt(f)?,
|
||||
PExpr::If(cond, res) => write!(f, "if {cond:?} then {res:?}")?,
|
||||
PExpr::Loop(res) => write!(f, "loop -> {res:?}")?,
|
||||
PExpr::Break => write!(f, "break")?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ impl MaybeParsable for PLiteral {
|
||||
if !first.is_ascii_digit() {
|
||||
return Ok(None);
|
||||
}
|
||||
let (whole, ty) = parse_whole_num(&text);
|
||||
let (whole, ty) = parse_whole_num(text);
|
||||
let mut num = PNumber {
|
||||
whole,
|
||||
decimal: None,
|
||||
|
||||
Reference in New Issue
Block a user