Add parsing of array type
This commit is contained in:
parent
4e8228f903
commit
887071eeb6
11
reid/examples/array.rs
Normal file
11
reid/examples/array.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
use reid::compile;
|
||||||
|
|
||||||
|
pub static ARRAY: &str = include_str!("./reid/array.reid");
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let text = match compile(ARRAY) {
|
||||||
|
Ok(t) => t,
|
||||||
|
Err(e) => panic!("{}", e),
|
||||||
|
};
|
||||||
|
println!("{}", text);
|
||||||
|
}
|
13
reid/examples/reid/array.reid
Normal file
13
reid/examples/reid/array.reid
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
// Arithmetic, function calls and imports!
|
||||||
|
|
||||||
|
fn array() -> [u16; 4] {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> u16 {
|
||||||
|
let heehoo = 10;
|
||||||
|
|
||||||
|
let list = array();
|
||||||
|
|
||||||
|
return heehoo;
|
||||||
|
}
|
@ -1,12 +0,0 @@
|
|||||||
// if-statements, functions
|
|
||||||
|
|
||||||
import std::print;
|
|
||||||
|
|
||||||
fn fibonacci(value: i32) -> i32 {
|
|
||||||
if value < 3 {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return fibonacci(value - 1) + fibonacci(value - 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
print(fibonacci(15));
|
|
@ -6,10 +6,10 @@ use crate::token_stream::TokenRange;
|
|||||||
pub mod parse;
|
pub mod parse;
|
||||||
pub mod process;
|
pub mod process;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Type(pub TypeKind, pub TokenRange);
|
pub struct Type(pub TypeKind, pub TokenRange);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum TypeKind {
|
pub enum TypeKind {
|
||||||
Bool,
|
Bool,
|
||||||
I8,
|
I8,
|
||||||
@ -22,6 +22,7 @@ pub enum TypeKind {
|
|||||||
U32,
|
U32,
|
||||||
U64,
|
U64,
|
||||||
U128,
|
U128,
|
||||||
|
Array(Box<TypeKind>, u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -13,24 +13,37 @@ where
|
|||||||
|
|
||||||
impl Parse for Type {
|
impl Parse for Type {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
let kind = if let Some(Token::Identifier(ident)) = stream.next() {
|
let kind = if let Some(Token::BracketOpen) = stream.peek() {
|
||||||
Ok(match &*ident {
|
stream.expect(Token::BracketOpen)?;
|
||||||
"bool" => TypeKind::Bool,
|
let inner = stream.parse::<Type>()?;
|
||||||
"i8" => TypeKind::I8,
|
stream.expect(Token::Semi)?;
|
||||||
"i16" => TypeKind::I16,
|
let length = if let Some(Token::DecimalValue(length)) = stream.next() {
|
||||||
"i32" => TypeKind::I32,
|
length
|
||||||
"i64" => TypeKind::I64,
|
} else {
|
||||||
"i128" => TypeKind::I128,
|
return Err(stream.expected_err("array length (number)")?);
|
||||||
"u8" => TypeKind::U8,
|
};
|
||||||
"u16" => TypeKind::U16,
|
stream.expect(Token::BracketClose)?;
|
||||||
"u32" => TypeKind::U32,
|
TypeKind::Array(Box::new(inner.0), length)
|
||||||
"u64" => TypeKind::U64,
|
|
||||||
"u128" => TypeKind::U128,
|
|
||||||
_ => panic!("asd"),
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
Err(stream.expected_err("type identifier")?)
|
if let Some(Token::Identifier(ident)) = stream.next() {
|
||||||
}?;
|
match &*ident {
|
||||||
|
"bool" => TypeKind::Bool,
|
||||||
|
"i8" => TypeKind::I8,
|
||||||
|
"i16" => TypeKind::I16,
|
||||||
|
"i32" => TypeKind::I32,
|
||||||
|
"i64" => TypeKind::I64,
|
||||||
|
"i128" => TypeKind::I128,
|
||||||
|
"u8" => TypeKind::U8,
|
||||||
|
"u16" => TypeKind::U16,
|
||||||
|
"u32" => TypeKind::U32,
|
||||||
|
"u64" => TypeKind::U64,
|
||||||
|
"u128" => TypeKind::U128,
|
||||||
|
_ => Err(stream.expected_err("known type identifier")?)?,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(stream.expected_err("type identifier")?)?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Type(kind, stream.get_range().unwrap()))
|
Ok(Type(kind, stream.get_range().unwrap()))
|
||||||
}
|
}
|
||||||
@ -68,7 +81,7 @@ impl Parse for PrimaryExpression {
|
|||||||
Expression(Kind::VariableName(v.clone()), stream.get_range().unwrap())
|
Expression(Kind::VariableName(v.clone()), stream.get_range().unwrap())
|
||||||
}
|
}
|
||||||
Token::DecimalValue(v) => Expression(
|
Token::DecimalValue(v) => Expression(
|
||||||
Kind::Literal(Literal::Number(v.parse().unwrap())),
|
Kind::Literal(Literal::Number(*v)),
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
),
|
),
|
||||||
Token::True => Expression(
|
Token::True => Expression(
|
||||||
|
@ -29,6 +29,7 @@ impl ast::Module {
|
|||||||
name: signature.name.clone(),
|
name: signature.name.clone(),
|
||||||
return_type: signature
|
return_type: signature
|
||||||
.return_type
|
.return_type
|
||||||
|
.clone()
|
||||||
.map(|r| r.0.into())
|
.map(|r| r.0.into())
|
||||||
.unwrap_or(mir::TypeKind::Void),
|
.unwrap_or(mir::TypeKind::Void),
|
||||||
parameters: signature
|
parameters: signature
|
||||||
@ -63,6 +64,7 @@ impl ast::Block {
|
|||||||
mir::VariableReference(
|
mir::VariableReference(
|
||||||
s_let
|
s_let
|
||||||
.1
|
.1
|
||||||
|
.clone()
|
||||||
.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(),
|
||||||
@ -181,7 +183,7 @@ impl ast::Literal {
|
|||||||
|
|
||||||
impl From<ast::TypeKind> for mir::TypeKind {
|
impl From<ast::TypeKind> for mir::TypeKind {
|
||||||
fn from(value: ast::TypeKind) -> Self {
|
fn from(value: ast::TypeKind) -> Self {
|
||||||
match value {
|
match &value {
|
||||||
ast::TypeKind::Bool => mir::TypeKind::Bool,
|
ast::TypeKind::Bool => mir::TypeKind::Bool,
|
||||||
ast::TypeKind::I8 => mir::TypeKind::I8,
|
ast::TypeKind::I8 => mir::TypeKind::I8,
|
||||||
ast::TypeKind::I16 => mir::TypeKind::I16,
|
ast::TypeKind::I16 => mir::TypeKind::I16,
|
||||||
@ -193,6 +195,7 @@ impl From<ast::TypeKind> for mir::TypeKind {
|
|||||||
ast::TypeKind::U32 => mir::TypeKind::U32,
|
ast::TypeKind::U32 => mir::TypeKind::U32,
|
||||||
ast::TypeKind::U64 => mir::TypeKind::U64,
|
ast::TypeKind::U64 => mir::TypeKind::U64,
|
||||||
ast::TypeKind::U128 => mir::TypeKind::U128,
|
ast::TypeKind::U128 => mir::TypeKind::U128,
|
||||||
|
ast::TypeKind::Array(type_kind, length) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ pub enum Token {
|
|||||||
// Values
|
// Values
|
||||||
Identifier(String),
|
Identifier(String),
|
||||||
/// Number with at most one decimal point
|
/// Number with at most one decimal point
|
||||||
DecimalValue(String),
|
DecimalValue(u64),
|
||||||
|
|
||||||
// Keywords
|
// Keywords
|
||||||
/// `let`
|
/// `let`
|
||||||
@ -62,6 +62,10 @@ pub enum Token {
|
|||||||
BraceOpen,
|
BraceOpen,
|
||||||
/// `}`
|
/// `}`
|
||||||
BraceClose,
|
BraceClose,
|
||||||
|
/// `[`
|
||||||
|
BracketOpen,
|
||||||
|
/// `]`
|
||||||
|
BracketClose,
|
||||||
/// `,`
|
/// `,`
|
||||||
Comma,
|
Comma,
|
||||||
|
|
||||||
@ -194,7 +198,7 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
value += &c.to_string();
|
value += &c.to_string();
|
||||||
cursor.next();
|
cursor.next();
|
||||||
}
|
}
|
||||||
Token::DecimalValue(value)
|
Token::DecimalValue(value.parse().expect("Decimal not parseable to u64"))
|
||||||
}
|
}
|
||||||
'-' if cursor.first() == Some('>') => {
|
'-' if cursor.first() == Some('>') => {
|
||||||
cursor.next(); // Eat `>`
|
cursor.next(); // Eat `>`
|
||||||
@ -213,6 +217,8 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
|||||||
'!' => Token::Exclamation,
|
'!' => Token::Exclamation,
|
||||||
'(' => Token::ParenOpen,
|
'(' => Token::ParenOpen,
|
||||||
')' => Token::ParenClose,
|
')' => Token::ParenClose,
|
||||||
|
'[' => Token::BracketOpen,
|
||||||
|
']' => Token::BracketClose,
|
||||||
'{' => Token::BraceOpen,
|
'{' => Token::BraceOpen,
|
||||||
'}' => Token::BraceClose,
|
'}' => Token::BraceClose,
|
||||||
',' => Token::Comma,
|
',' => Token::Comma,
|
||||||
|
Loading…
Reference in New Issue
Block a user