From 8297c192acb69cb49226241bb609fb1cd3a32ae7 Mon Sep 17 00:00:00 2001 From: ondra05 Date: Fri, 3 Jun 2022 00:10:19 +0200 Subject: [PATCH] Changed dim syntax --- ablescript/src/interpret.rs | 4 +-- ablescript/src/parser.rs | 52 ++++++++++++++++++++------------- examples/by-arity-chain.able | 8 ++--- examples/carts.able | 2 +- examples/hello-world.able | 2 +- examples/iotest.able | 2 +- examples/melo-hello.able | 2 +- examples/pass-by-reference.able | 6 ++-- 8 files changed, 44 insertions(+), 34 deletions(-) diff --git a/ablescript/src/interpret.rs b/ablescript/src/interpret.rs index f0cbfbe..3fb82c7 100644 --- a/ablescript/src/interpret.rs +++ b/ablescript/src/interpret.rs @@ -707,7 +707,7 @@ mod tests { let mut env = ExecEnv::new(); // 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!( env.get_var_value(&Spanned { item: "bar".to_owned(), @@ -741,7 +741,7 @@ mod tests { let mut env = ExecEnv::new(); eval( &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(); diff --git a/ablescript/src/parser.rs b/ablescript/src/parser.rs index 17726e5..def257b 100644 --- a/ablescript/src/parser.rs +++ b/ablescript/src/parser.rs @@ -75,7 +75,6 @@ impl<'source> Parser<'source> { 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( self.melo_flow()?, 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 => { return match buf.take() { Some(expr) => self.assignment_flow(expr), @@ -550,20 +574,6 @@ impl<'source> Parser<'source> { Ok(Stmt::Call { expr, args }) } - /// Parse variable declaration - fn dim_flow(&mut self) -> Result { - 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 fn assignment_flow(&mut self, value: Spanned) -> Result { let ident = self.get_ident()?; @@ -702,16 +712,16 @@ mod tests { #[test] fn variable_declaration() { - let code = "dim var 42;"; + let code = "var dim 42;"; let expected = &[Spanned { item: Stmt::Dim { ident: Spanned { item: "var".to_owned(), - span: 4..5, + span: 0..3, }, init: Some(Spanned { item: Expr::Literal(Literal::Int(42)), - span: 8..10, + span: 4..6, }), }, span: 0..11, @@ -760,12 +770,12 @@ mod tests { #[test] fn tdark() { - let code = "T-Dark { dim lang /*lang*/ + lang; }"; + let code = "T-Dark { lang dim /*lang*/ + lang; }"; let expected = &[Spanned { item: Stmt::Dim { ident: Spanned { item: "script".to_owned(), - span: 13..17, + span: 9..15, }, init: Some(Spanned { item: Expr::BinOp { diff --git a/examples/by-arity-chain.able b/examples/by-arity-chain.able index f34b6ca..2647a63 100644 --- a/examples/by-arity-chain.able +++ b/examples/by-arity-chain.able @@ -25,20 +25,20 @@ owo arity_1(/*foo*/); owo arity_2(/*foo*/, /*bar*/); owo arity_3(/*foo*/, /*bar*/, /*baz*/); -dim i1 arity_0 * arity_1; +i1 dim arity_0 * arity_1; i1(/*second*/); /*----*/ print; -dim i2 arity_1 * arity_0; +i2 dim arity_1 * arity_0; i2(/*first*/); /*----*/ print; -dim ifancy arity_3 * arity_3; +ifancy dim arity_3 * arity_3; ifancy(/*left1*/, /*right1*/, /*left2*/, /*right2*/, /*left3*/, /*right3*/); /*---*/ print; -dim another arity_0 * arity_3; +another dim arity_0 * arity_3; another(/*right1*/, /*right2*/, /*right3*/); diff --git a/examples/carts.able b/examples/carts.able index 1188ddb..65e9505 100644 --- a/examples/carts.able +++ b/examples/carts.able @@ -2,7 +2,7 @@ functio helloable() { /*Hello, Able!*/ print; } -dim cart [/*able*/ <= 42, helloable <= /*hello*/]; +cart dim [/*able*/ <= 42, helloable <= /*hello*/]; cart[42] print; cart[/*hello*/](); diff --git a/examples/hello-world.able b/examples/hello-world.able index 11778b4..247f8a4 100644 --- a/examples/hello-world.able +++ b/examples/hello-world.able @@ -1,2 +1,2 @@ -dim hello /*world*/; +hello dim /*world*/; hello print; diff --git a/examples/iotest.able b/examples/iotest.able index ca312e1..c3da0b4 100644 --- a/examples/iotest.able +++ b/examples/iotest.able @@ -1,4 +1,4 @@ -dim data; +data dim; loop { data read; data print; diff --git a/examples/melo-hello.able b/examples/melo-hello.able index b50b808..9224b6b 100644 --- a/examples/melo-hello.able +++ b/examples/melo-hello.able @@ -1,3 +1,3 @@ -dim hi /*wonk*/; +hi dim /*wonk*/; melo hi; hi print; owo Should error out diff --git a/examples/pass-by-reference.able b/examples/pass-by-reference.able index cde0c37..0d33139 100644 --- a/examples/pass-by-reference.able +++ b/examples/pass-by-reference.able @@ -2,13 +2,13 @@ owo Pass-by-reference test owo Swap two variables. functio swap(left, right) { - dim tmp left; + tmp dim left; right =: left; tmp =: right; } -dim foo /*hello*/; -dim bar /*world*/; +foo dim /*hello*/; +bar dim /*world*/; swap(foo, bar);