1
0
Fork 0
forked from AbleOS/ableos

Compare commits

...

2 commits

2 changed files with 76 additions and 5 deletions

View file

@ -22,6 +22,8 @@ enum Token {
// Tokens can be literal strings, of any length. // Tokens can be literal strings, of any length.
#[token("type")] #[token("type")]
Type, Type,
#[token("fn", priority = 5)]
Fn,
#[token("enum")] #[token("enum")]
Enum, Enum,
@ -56,7 +58,7 @@ enum Token {
#[token("->")] #[token("->")]
RArrow, RArrow,
#[regex("[a-zA-Z_]+", |lex|{lex.slice().to_string()})] #[regex("[a-zA-Z_][a-zA-Z_1234567890]+", |lex|{lex.slice().to_string()})]
Identifier(String), Identifier(String),
#[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})] #[regex("[1234567890]+", |lex|{lex.slice().parse::<u64>().unwrap()})]
@ -86,9 +88,6 @@ pub fn build_idl(name: String) {
println!("{:#?}", parse(tokens)); println!("{:#?}", parse(tokens));
} }
fn build(a: Vec<Token>) {
}
fn open_protocol(name: String) -> String { fn open_protocol(name: String) -> String {
let path = format!("sysdata/idl/{}/src/protocol.aldi", name); let path = format!("sysdata/idl/{}/src/protocol.aldi", name);
let mut file = std::fs::File::open(path).unwrap(); let mut file = std::fs::File::open(path).unwrap();

View file

@ -9,7 +9,7 @@ enum Declaration{
EnumDeclaration(EnumDeclaration), EnumDeclaration(EnumDeclaration),
StructDeclaration(StructDeclaration), StructDeclaration(StructDeclaration),
TypeDeclaration(TypeDeclaration), TypeDeclaration(TypeDeclaration),
ProtocolDeclaration, ProtocolDeclaration(ProtocolDeclaration),
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct StructDeclaration { struct StructDeclaration {
@ -38,6 +38,19 @@ struct EnumMember {
number: u64, number: u64,
} }
#[derive(Debug, Clone, PartialEq)]
struct ProtocolDeclaration{
name : String,
interface : Vec<FuncDeclaration>,
}
#[derive(Debug, Clone, PartialEq)]
struct FuncDeclaration {
name : String,
arg_list : Vec<String>,
return_type : String,
}
/// Consume's a token that's expected. If the token that's consumed is not /// Consume's a token that's expected. If the token that's consumed is not
/// the given expected token then panic /// the given expected token then panic
@ -74,6 +87,7 @@ fn declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<Declaration> {
Token::Enum => Some(enum_decl(tokens)), Token::Enum => Some(enum_decl(tokens)),
Token::Struct => Some(struct_decl(tokens)), Token::Struct => Some(struct_decl(tokens)),
Token::Type => Some(type_declaration(tokens)), Token::Type => Some(type_declaration(tokens)),
Token::Protocol => Some(protocol_declaration(tokens)),
_ => None, _ => None,
} }
} }
@ -200,3 +214,61 @@ fn parse_number(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<u64> {
} }
result result
} }
fn protocol_declaration(tokens : &mut Peekable<Iter<'_, Token>>) -> Declaration {
consume(tokens, Token::Protocol);
let name = identifier(tokens).expect("Expected Identifier after `protocol`");
consume(tokens, Token::LBrace);
let mut interface = Vec::new();
match tokens.peek().expect("Unexpected EOF after LBrace") {
Token::RBrace => {},
_ => {
functions(tokens, &mut interface);
},
};
Declaration::ProtocolDeclaration(ProtocolDeclaration{name, interface})
}
fn functions(tokens : &mut Peekable<Iter<'_, Token>>, interface : &mut Vec<FuncDeclaration>) {
loop {
match function(tokens) {
Some(x) => interface.push(x),
None => break,
}
}
}
fn function(tokens : &mut Peekable<Iter<'_, Token>>) -> Option<FuncDeclaration>{
if let Some(Token::Fn) = tokens.peek() {
consume(tokens, Token::Fn);
let name = identifier(tokens).expect("Expected Identifier after `fn`");
consume(tokens, Token::LParen);
let arg_list = arg_list(tokens);
consume(tokens, Token::RParen);
consume(tokens, Token::RArrow);
let return_type = identifier(tokens).expect("Expected return type after `->");
Some(FuncDeclaration{name, arg_list, return_type})
} else {
None
}
}
fn arg_list(tokens : &mut Peekable<Iter<'_, Token>>) -> Vec<String> {
let mut result = Vec::new();
loop {
match identifier(tokens) {
None => break,
Some(i) =>{
result.push(i);
if **tokens.peek().expect("Unexpected EOF in argument list") == Token::Comma{
consume(tokens, Token::Comma);
match tokens.peek().expect("Unexpected EOF in argument list") {
Token::Identifier(_) => {},
_ => panic!("Unexpected symbol after Comma in argument list"),
}
}
}
}
}
result
}