Parse if-statement
This commit is contained in:
parent
5ed3edd433
commit
b3fec86fbd
@ -1,7 +1,7 @@
|
|||||||
function max (a, b)
|
function max (a, b)
|
||||||
local m = a
|
local m = a
|
||||||
-- if b > a then
|
if a then
|
||||||
-- m = b
|
local m = b
|
||||||
-- end
|
end
|
||||||
return m
|
return m
|
||||||
end
|
end
|
||||||
@ -203,6 +203,7 @@ impl Parse for Block {
|
|||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Assignment(Option<DefinitionKind>, Node<String>, Node<Expression>),
|
Assignment(Option<DefinitionKind>, Node<String>, Node<Expression>),
|
||||||
Return(Node<Expression>),
|
Return(Node<Expression>),
|
||||||
|
If(Node<Expression>, Block),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for Statement {
|
impl Parse for Statement {
|
||||||
@ -211,6 +212,13 @@ impl Parse for Statement {
|
|||||||
if peeked == Some(Token::Keyword(Keyword::Return)) {
|
if peeked == Some(Token::Keyword(Keyword::Return)) {
|
||||||
stream.next();
|
stream.next();
|
||||||
Ok(Statement::Return(stream.parse()?))
|
Ok(Statement::Return(stream.parse()?))
|
||||||
|
} else if peeked == Some(Token::Keyword(Keyword::If)) {
|
||||||
|
stream.next(); // Consume if
|
||||||
|
let cond = stream.parse()?;
|
||||||
|
stream.expect(Token::Keyword(Keyword::Then))?;
|
||||||
|
let then = stream.parse()?;
|
||||||
|
stream.expect(Token::Keyword(Keyword::End))?;
|
||||||
|
Ok(Self::If(cond, then))
|
||||||
} else if peeked == Some(Token::Keyword(Keyword::Local)) {
|
} else if peeked == Some(Token::Keyword(Keyword::Local)) {
|
||||||
stream.next();
|
stream.next();
|
||||||
let name = stream.parse()?;
|
let name = stream.parse()?;
|
||||||
|
|||||||
@ -18,6 +18,8 @@ fn main() {
|
|||||||
let tokens = tokenize(TEST).unwrap();
|
let tokens = tokenize(TEST).unwrap();
|
||||||
let mut stream = TokenStream::from(&file_path, &tokens);
|
let mut stream = TokenStream::from(&file_path, &tokens);
|
||||||
|
|
||||||
|
dbg!(&tokens);
|
||||||
|
|
||||||
let mut functions = Vec::new();
|
let mut functions = Vec::new();
|
||||||
while stream.peek() != Some(Token::Eof) {
|
while stream.peek() != Some(Token::Eof) {
|
||||||
functions.push(stream.parse::<Function>().unwrap());
|
functions.push(stream.parse::<Function>().unwrap());
|
||||||
|
|||||||
@ -13,6 +13,8 @@ pub enum Keyword {
|
|||||||
End,
|
End,
|
||||||
Local,
|
Local,
|
||||||
Return,
|
Return,
|
||||||
|
If,
|
||||||
|
Then,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Keyword {
|
impl Keyword {
|
||||||
@ -22,6 +24,8 @@ impl Keyword {
|
|||||||
"end" => Keyword::End,
|
"end" => Keyword::End,
|
||||||
"local" => Keyword::Local,
|
"local" => Keyword::Local,
|
||||||
"return" => Keyword::Return,
|
"return" => Keyword::Return,
|
||||||
|
"if" => Keyword::If,
|
||||||
|
"then" => Keyword::Then,
|
||||||
_ => None?,
|
_ => None?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -34,6 +38,8 @@ impl ToString for Keyword {
|
|||||||
Keyword::End => "end",
|
Keyword::End => "end",
|
||||||
Keyword::Local => "local",
|
Keyword::Local => "local",
|
||||||
Keyword::Return => "return",
|
Keyword::Return => "return",
|
||||||
|
Keyword::If => "if",
|
||||||
|
Keyword::Then => "then",
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user