Changed dim syntax

This commit is contained in:
Erin 2022-06-03 00:10:19 +02:00 committed by ondra05
parent 2c7596c871
commit 84f54f12d6
8 changed files with 44 additions and 34 deletions

View file

@ -707,7 +707,7 @@ mod tests {
let mut env = ExecEnv::new(); let mut env = ExecEnv::new();
// Declaring and reading from a variable. // Declaring and reading from a variable.
eval(&mut env, "dim foo 32; dim bar foo + 1;").unwrap(); eval(&mut env, "foo dim 32; bar dim foo + 1;").unwrap();
assert_eq!( assert_eq!(
env.get_var_value(&Spanned { env.get_var_value(&Spanned {
item: "bar".to_owned(), item: "bar".to_owned(),
@ -741,7 +741,7 @@ mod tests {
let mut env = ExecEnv::new(); let mut env = ExecEnv::new();
eval( eval(
&mut env, &mut env,
"dim foo 1; 2 =: foo; unless (never) { dim foo 3; 4 =: foo; }", "foo dim 1; 2 =: foo; unless (never) { foo dim 3; 4 =: foo; }",
) )
.unwrap(); .unwrap();

View file

@ -75,7 +75,6 @@ impl<'source> Parser<'source> {
start..self.lexer.span().end, start..self.lexer.span().end,
)), )),
Token::Bff => Ok(Spanned::new(self.bff_flow()?, start..self.lexer.span().end)), Token::Bff => Ok(Spanned::new(self.bff_flow()?, start..self.lexer.span().end)),
Token::Dim => Ok(Spanned::new(self.dim_flow()?, start..self.lexer.span().end)),
Token::Melo => Ok(Spanned::new( Token::Melo => Ok(Spanned::new(
self.melo_flow()?, self.melo_flow()?,
start..self.lexer.span().end, start..self.lexer.span().end,
@ -393,7 +392,32 @@ impl<'source> Parser<'source> {
})?)?; })?)?;
} }
// Variable Assignment // Variable declaration
Token::Dim => {
return match buf.take() {
Some(Spanned {
item: Expr::Variable(ident),
span,
}) => Ok(Stmt::Dim {
ident: Spanned::new(ident, span),
init: {
let mut init = None;
loop {
match self.checked_next()? {
Token::Semicolon => break init,
token => init = Some(self.parse_expr(token, &mut init)?),
}
}
},
}),
_ => Err(Error::new(
ErrorKind::UnexpectedToken(Token::Dim),
self.lexer.span(),
)),
}
}
// Variable assignment
Token::Assign => { Token::Assign => {
return match buf.take() { return match buf.take() {
Some(expr) => self.assignment_flow(expr), Some(expr) => self.assignment_flow(expr),
@ -550,20 +574,6 @@ impl<'source> Parser<'source> {
Ok(Stmt::Call { expr, args }) Ok(Stmt::Call { expr, args })
} }
/// Parse variable declaration
fn dim_flow(&mut self) -> Result<Stmt, Error> {
let ident = self.get_ident()?;
let mut init = None;
loop {
match self.checked_next()? {
Token::Semicolon => break,
t => init = Some(self.parse_expr(t, &mut init)?),
}
}
Ok(Stmt::Dim { ident, init })
}
/// Parse assignment to assignable /// Parse assignment to assignable
fn assignment_flow(&mut self, value: Spanned<Expr>) -> Result<Stmt, Error> { fn assignment_flow(&mut self, value: Spanned<Expr>) -> Result<Stmt, Error> {
let ident = self.get_ident()?; let ident = self.get_ident()?;
@ -702,16 +712,16 @@ mod tests {
#[test] #[test]
fn variable_declaration() { fn variable_declaration() {
let code = "dim var 42;"; let code = "var dim 42;";
let expected = &[Spanned { let expected = &[Spanned {
item: Stmt::Dim { item: Stmt::Dim {
ident: Spanned { ident: Spanned {
item: "var".to_owned(), item: "var".to_owned(),
span: 4..5, span: 0..3,
}, },
init: Some(Spanned { init: Some(Spanned {
item: Expr::Literal(Literal::Int(42)), item: Expr::Literal(Literal::Int(42)),
span: 8..10, span: 4..6,
}), }),
}, },
span: 0..11, span: 0..11,
@ -760,12 +770,12 @@ mod tests {
#[test] #[test]
fn tdark() { fn tdark() {
let code = "T-Dark { dim lang /*lang*/ + lang; }"; let code = "T-Dark { lang dim /*lang*/ + lang; }";
let expected = &[Spanned { let expected = &[Spanned {
item: Stmt::Dim { item: Stmt::Dim {
ident: Spanned { ident: Spanned {
item: "script".to_owned(), item: "script".to_owned(),
span: 13..17, span: 9..15,
}, },
init: Some(Spanned { init: Some(Spanned {
item: Expr::BinOp { item: Expr::BinOp {

View file

@ -25,20 +25,20 @@ owo arity_1(/*foo*/);
owo arity_2(/*foo*/, /*bar*/); owo arity_2(/*foo*/, /*bar*/);
owo arity_3(/*foo*/, /*bar*/, /*baz*/); owo arity_3(/*foo*/, /*bar*/, /*baz*/);
dim i1 arity_0 * arity_1; i1 dim arity_0 * arity_1;
i1(/*second*/); i1(/*second*/);
/*----*/ print; /*----*/ print;
dim i2 arity_1 * arity_0; i2 dim arity_1 * arity_0;
i2(/*first*/); i2(/*first*/);
/*----*/ print; /*----*/ print;
dim ifancy arity_3 * arity_3; ifancy dim arity_3 * arity_3;
ifancy(/*left1*/, /*right1*/, /*left2*/, /*right2*/, /*left3*/, /*right3*/); ifancy(/*left1*/, /*right1*/, /*left2*/, /*right2*/, /*left3*/, /*right3*/);
/*---*/ print; /*---*/ print;
dim another arity_0 * arity_3; another dim arity_0 * arity_3;
another(/*right1*/, /*right2*/, /*right3*/); another(/*right1*/, /*right2*/, /*right3*/);

View file

@ -2,7 +2,7 @@ functio helloable() {
/*Hello, Able!*/ print; /*Hello, Able!*/ print;
} }
dim cart [/*able*/ <= 42, helloable <= /*hello*/]; cart dim [/*able*/ <= 42, helloable <= /*hello*/];
cart[42] print; cart[42] print;
cart[/*hello*/](); cart[/*hello*/]();

View file

@ -1,2 +1,2 @@
dim hello /*world*/; hello dim /*world*/;
hello print; hello print;

View file

@ -1,4 +1,4 @@
dim data; data dim;
loop { loop {
data read; data read;
data print; data print;

View file

@ -1,3 +1,3 @@
dim hi /*wonk*/; hi dim /*wonk*/;
melo hi; melo hi;
hi print; owo Should error out hi print; owo Should error out

View file

@ -2,13 +2,13 @@ owo Pass-by-reference test
owo Swap two variables. owo Swap two variables.
functio swap(left, right) { functio swap(left, right) {
dim tmp left; tmp dim left;
right =: left; right =: left;
tmp =: right; tmp =: right;
} }
dim foo /*hello*/; foo dim /*hello*/;
dim bar /*world*/; bar dim /*world*/;
swap(foo, bar); swap(foo, bar);