1
0
Fork 0
forked from AbleOS/ableos

Added types

This commit is contained in:
Talha Qamar 2024-09-14 05:57:18 +05:00
parent 0775d0c70a
commit d0d6f0475e
4 changed files with 97 additions and 23 deletions

View file

@ -1,4 +1,6 @@
mod parser;
mod types;
mod protocol;
use crate::idl::parser::parse;
use std::io::Read;

View file

@ -2,10 +2,10 @@ use std::{iter::Peekable, slice::Iter};
use super::Token;
#[derive(Debug, Clone, PartialEq)]
pub struct AST(Vec<Declaration>);
pub struct AST(pub Vec<Declaration>);
#[derive(Debug, Clone, PartialEq)]
enum Declaration{
pub enum Declaration{
EnumDeclaration(EnumDeclaration),
StructDeclaration(StructDeclaration),
TypeDeclaration(TypeDeclaration),
@ -13,40 +13,40 @@ enum Declaration{
}
#[derive(Debug, Clone, PartialEq)]
struct StructDeclaration {
name : String,
members : Vec<StructMember>,
pub struct StructDeclaration {
pub name : String,
pub members : Vec<StructMember>,
}
#[derive(Debug, Clone, PartialEq)]
struct TypeDeclaration {
name : String,
type_name : String,
pub struct TypeDeclaration {
pub name : String,
pub type_name : String,
}
#[derive(Debug, Clone, PartialEq)]
struct StructMember {
name : String,
type_name : String,
pub struct StructMember {
pub name : String,
pub type_name : String,
}
#[derive(Debug, Clone, PartialEq)]
struct EnumDeclaration {
name : String,
members : Vec<EnumMember>,
pub struct EnumDeclaration {
pub name : String,
pub members : Vec<EnumMember>,
}
#[derive(Debug, Clone, PartialEq)]
struct EnumMember {
name : String,
number: u64,
pub struct EnumMember {
pub name : String,
pub number: u64,
}
#[derive(Debug, Clone, PartialEq)]
struct ProtocolDeclaration{
pub struct ProtocolDeclaration{
name : String,
interface : Vec<FuncDeclaration>,
}
#[derive(Debug, Clone, PartialEq)]
struct FuncDeclaration {
pub struct FuncDeclaration {
name : String,
arg_list : Vec<String>,
return_type : String,

View file

@ -1,8 +1,5 @@
pub enum ProtocolTypes {
Byte,
pub struct Protocol {
}
pub struct Protocol {}
impl Protocol {
pub fn is_empty(&self) -> bool {
true

75
dev/src/idl/types.rs Normal file
View file

@ -0,0 +1,75 @@
use std::collections::HashMap;
use crate::idl::protocol::Protocol;
use crate::idl::parser::AST;
#[derive(Debug, Clone, PartialEq)]
enum Type {
U64,
U32,
U16,
U8,
I64,
I32,
I16,
I8,
Bool,
F32,
F64,
Struct(StructType),
Enum(EnumType),
Alias(String),
}
#[derive(Debug, Clone, PartialEq)]
struct StructType {
members : HashMap<String, String>
}
#[derive(Debug, Clone, PartialEq)]
struct EnumType {
members : HashMap<String, u8>
}
pub fn get_protocols(ast : AST) -> Vec<Protocol> {
let protocols : Vec<Protocol> = Vec::new();
let mut symbol_table : HashMap<String, Type> = HashMap::new();
let declarations = ast.0;
// First Pass
// We just populate the symbol table here
for decl in declarations.iter() {
match decl{
super::parser::Declaration::EnumDeclaration(e) => {
let mut members = HashMap::new();
for m in e.members.iter(){
members.insert(m.name.to_string(), m.number as u8);
}
symbol_table.insert(e.name.to_string(), Type::Enum(EnumType{members}));
},
super::parser::Declaration::StructDeclaration(s) => {
let mut members = HashMap::new();
for m in s.members.iter() {
members.insert(m.name.to_string(), m.type_name.to_string());
}
symbol_table.insert(s.name.to_string(), Type::Struct(StructType{members}));
},
super::parser::Declaration::TypeDeclaration(t) => {
symbol_table.insert(t.name.to_string(), Type::Alias(t.type_name.to_string()));
},
super::parser::Declaration::ProtocolDeclaration(_) => {},
}
}
for decl in declarations.iter(){
match decl {
super::parser::Declaration::ProtocolDeclaration(p) => {
},
_ => {}
}
}
protocols
}