Add mutability parsing
This commit is contained in:
parent
615fec6e52
commit
85b2ebf04a
@ -1,9 +1,9 @@
|
|||||||
use reid::compile;
|
use reid::compile;
|
||||||
|
|
||||||
pub static ARITHMETIC: &str = include_str!("./reid/arithmetic.reid");
|
pub static MUTABLE: &str = include_str!("./reid/mutable.reid");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let text = match compile(ARITHMETIC) {
|
let text = match compile(MUTABLE) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
};
|
};
|
@ -1,13 +0,0 @@
|
|||||||
// Arithmetic, function calls and imports!
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let test = 9;
|
|
||||||
let simpleAdd = 2 + 2;
|
|
||||||
let simpleSub = 7 - 2; // 14
|
|
||||||
|
|
||||||
if simpleAdd < test {
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
return arithmetic + simpleSub + boop;
|
|
||||||
}
|
|
15
reid/examples/reid/mutable.reid
Normal file
15
reid/examples/reid/mutable.reid
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
fn indirection() -> bool {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> u16 {
|
||||||
|
let mut test = 5;
|
||||||
|
|
||||||
|
if indirection() {
|
||||||
|
test = 11;
|
||||||
|
}
|
||||||
|
|
||||||
|
return test;
|
||||||
|
}
|
@ -92,7 +92,14 @@ pub struct IfExpression(
|
|||||||
);
|
);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct LetStatement(pub String, pub Option<Type>, pub Expression, pub TokenRange);
|
pub struct LetStatement(
|
||||||
|
pub String,
|
||||||
|
pub Option<Type>,
|
||||||
|
/// Mutability
|
||||||
|
pub bool,
|
||||||
|
pub Expression,
|
||||||
|
pub TokenRange,
|
||||||
|
);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ImportStatement(pub Vec<String>, pub TokenRange);
|
pub struct ImportStatement(pub Vec<String>, pub TokenRange);
|
||||||
@ -125,7 +132,11 @@ pub struct Block(
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum BlockLevelStatement {
|
pub enum BlockLevelStatement {
|
||||||
Let(LetStatement),
|
Let(LetStatement),
|
||||||
Import { _i: ImportStatement },
|
/// Try to set a variable to a specified expression value
|
||||||
|
Set(String, Expression),
|
||||||
|
Import {
|
||||||
|
_i: ImportStatement,
|
||||||
|
},
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
Return(ReturnType, Expression),
|
Return(ReturnType, Expression),
|
||||||
}
|
}
|
||||||
|
@ -223,6 +223,7 @@ impl Parse for IfExpression {
|
|||||||
impl Parse for LetStatement {
|
impl Parse for LetStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<LetStatement, Error> {
|
fn parse(mut stream: TokenStream) -> Result<LetStatement, Error> {
|
||||||
stream.expect(Token::LetKeyword)?;
|
stream.expect(Token::LetKeyword)?;
|
||||||
|
let mutability = stream.expect(Token::MutKeyword).is_ok();
|
||||||
|
|
||||||
if let Some(Token::Identifier(variable)) = stream.next() {
|
if let Some(Token::Identifier(variable)) = stream.next() {
|
||||||
stream.expect(Token::Equals)?;
|
stream.expect(Token::Equals)?;
|
||||||
@ -232,6 +233,7 @@ impl Parse for LetStatement {
|
|||||||
Ok(LetStatement(
|
Ok(LetStatement(
|
||||||
variable,
|
variable,
|
||||||
None, // TODO add possibility to name type
|
None, // TODO add possibility to name type
|
||||||
|
mutability,
|
||||||
expression,
|
expression,
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
))
|
))
|
||||||
@ -361,6 +363,9 @@ impl Parse for BlockLevelStatement {
|
|||||||
Stmt::Return(ReturnType::Hard, exp)
|
Stmt::Return(ReturnType::Hard, exp)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
if let Ok(SetStatement(ident, expr)) = stream.parse() {
|
||||||
|
Stmt::Set(ident, expr)
|
||||||
|
} else {
|
||||||
if let Ok(e) = stream.parse() {
|
if let Ok(e) = stream.parse() {
|
||||||
if stream.expect(Token::Semi).is_ok() {
|
if stream.expect(Token::Semi).is_ok() {
|
||||||
Stmt::Expression(e)
|
Stmt::Expression(e)
|
||||||
@ -371,10 +376,27 @@ impl Parse for BlockLevelStatement {
|
|||||||
Err(stream.expected_err("expression")?)?
|
Err(stream.expected_err("expression")?)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SetStatement(String, Expression);
|
||||||
|
|
||||||
|
impl Parse for SetStatement {
|
||||||
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
|
if let Some(Token::Identifier(ident)) = stream.next() {
|
||||||
|
stream.expect(Token::Equals)?;
|
||||||
|
let expr = stream.parse()?;
|
||||||
|
stream.expect(Token::Semi)?;
|
||||||
|
Ok(Self(ident, expr))
|
||||||
|
} else {
|
||||||
|
Err(stream.expected_err("identifier")?)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for TopLevelStatement {
|
impl Parse for TopLevelStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
use TopLevelStatement as Stmt;
|
use TopLevelStatement as Stmt;
|
||||||
|
@ -68,12 +68,13 @@ impl ast::Block {
|
|||||||
.map(|t| t.0.into())
|
.map(|t| t.0.into())
|
||||||
.unwrap_or(mir::TypeKind::Vague(mir::VagueType::Unknown)),
|
.unwrap_or(mir::TypeKind::Vague(mir::VagueType::Unknown)),
|
||||||
s_let.0.clone(),
|
s_let.0.clone(),
|
||||||
s_let.3.into(),
|
s_let.4.into(),
|
||||||
),
|
),
|
||||||
s_let.2.process(),
|
s_let.3.process(),
|
||||||
),
|
),
|
||||||
s_let.3,
|
s_let.4,
|
||||||
),
|
),
|
||||||
|
ast::BlockLevelStatement::Set(_, expression) => todo!(),
|
||||||
ast::BlockLevelStatement::Import { _i } => todo!(),
|
ast::BlockLevelStatement::Import { _i } => todo!(),
|
||||||
ast::BlockLevelStatement::Expression(e) => (StmtKind::Expression(e.process()), e.1),
|
ast::BlockLevelStatement::Expression(e) => (StmtKind::Expression(e.process()), e.1),
|
||||||
ast::BlockLevelStatement::Return(_, e) => (StmtKind::Expression(e.process()), e.1),
|
ast::BlockLevelStatement::Return(_, e) => (StmtKind::Expression(e.process()), e.1),
|
||||||
|
@ -12,6 +12,8 @@ pub enum Token {
|
|||||||
// Keywords
|
// Keywords
|
||||||
/// `let`
|
/// `let`
|
||||||
LetKeyword,
|
LetKeyword,
|
||||||
|
/// `mut`
|
||||||
|
MutKeyword,
|
||||||
/// `import`
|
/// `import`
|
||||||
ImportKeyword,
|
ImportKeyword,
|
||||||
/// `return`
|
/// `return`
|
||||||
@ -170,6 +172,7 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
// Check for keywords
|
// Check for keywords
|
||||||
let variant = match value.as_str() {
|
let variant = match value.as_str() {
|
||||||
"let" => Token::LetKeyword,
|
"let" => Token::LetKeyword,
|
||||||
|
"mut" => Token::MutKeyword,
|
||||||
"import" => Token::ImportKeyword,
|
"import" => Token::ImportKeyword,
|
||||||
"return" => Token::ReturnKeyword,
|
"return" => Token::ReturnKeyword,
|
||||||
"fn" => Token::FnKeyword,
|
"fn" => Token::FnKeyword,
|
||||||
|
Loading…
Reference in New Issue
Block a user