From c9fea374302cb51c69957a9552fcc306404923ee Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Wed, 8 Jul 2020 14:46:31 +0200 Subject: [PATCH] Add inital ast for glsl-new (#107) * Add inital ast for glsl-new * Fix clippy for glsl-new Deref instead of cloning --- src/front/glsl_new/ast.rs | 34 +++++++++++++++++++++++++++++++ src/front/glsl_new/mod.rs | 39 +++++++++++++----------------------- src/front/glsl_new/parser.rs | 15 ++++++++++---- 3 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 src/front/glsl_new/ast.rs diff --git a/src/front/glsl_new/ast.rs b/src/front/glsl_new/ast.rs new file mode 100644 index 0000000000..1f06439121 --- /dev/null +++ b/src/front/glsl_new/ast.rs @@ -0,0 +1,34 @@ +use crate::{Arena, FastHashMap, Function, Handle}; + +#[derive(Debug)] +pub struct Program { + pub version: u16, + pub profile: Profile, + pub ext_decls: Vec, + pub lookup_function: FastHashMap>, + pub functions: Arena, +} + +impl Program { + pub fn new() -> Program { + Program { + version: 0, + profile: Profile::Core, + ext_decls: vec![], + lookup_function: FastHashMap::default(), + functions: Arena::::new(), + } + } +} + +#[derive(Debug)] +pub enum Profile { + // Compatibility, + Core, + // Es, +} + +#[derive(Debug)] +pub enum ExtDecl { + // FunctionDecl, +} diff --git a/src/front/glsl_new/mod.rs b/src/front/glsl_new/mod.rs index 202a7e6fa0..6c1fb568fe 100644 --- a/src/front/glsl_new/mod.rs +++ b/src/front/glsl_new/mod.rs @@ -1,11 +1,12 @@ -use crate::{ - Arena, Constant, EntryPoint, Function, GlobalVariable, Header, Module, ShaderStage, Type, -}; +use crate::{EntryPoint, Module, ShaderStage}; mod lex; #[cfg(test)] mod lex_tests; +mod ast; +use ast::Program; + use lex::Lexer; mod error; use error::ParseError; @@ -15,38 +16,26 @@ mod token; pub fn parse_str(source: &str, entry: String, stage: ShaderStage) -> Result { log::debug!("------ GLSL-pomelo ------"); - let module = Module { - header: Header { - version: (1, 0, 0), - generator: 0, - }, - types: Arena::::new(), - constants: Arena::::new(), - global_variables: Arena::::new(), - functions: Arena::::new(), - entry_points: vec![], - }; - + let mut program = Program::new(); let lex = Lexer::new(source); - let mut parser = parser::Parser::new(module); + let mut parser = parser::Parser::new(&mut program); for token in lex { parser.parse(token)?; } - let (_, mut parsed_module) = parser.end_of_input()?; + parser.end_of_input()?; + + let mut module = Module::generate_empty(); + module.functions = program.functions; // find entry point - let entry_func = parsed_module - .functions - .iter() - .find(|(_, f)| f.name.as_ref().filter(|n| **n == entry).is_some()); - if let Some((h, _)) = entry_func { - parsed_module.entry_points.push(EntryPoint { + if let Some(entry_handle) = program.lookup_function.get(&entry) { + module.entry_points.push(EntryPoint { stage, name: entry, - function: h, + function: *entry_handle, }); } - Ok(parsed_module) + Ok(module) } diff --git a/src/front/glsl_new/parser.rs b/src/front/glsl_new/parser.rs index ee1c89b906..9cba9a6b67 100644 --- a/src/front/glsl_new/parser.rs +++ b/src/front/glsl_new/parser.rs @@ -5,11 +5,12 @@ use pomelo::pomelo; pomelo! { //%verbose; %include { - use super::super::{error::ErrorKind, token::*}; - use crate::{Arena, Expression, Function, LocalVariable, Module}; + use super::super::{error::ErrorKind, token::*, ast::*}; + use crate::{Arena, Expression, Function, LocalVariable}; } %token #[derive(Debug)] pub enum Token {}; - %extra_argument Module; + %parser pub struct Parser<'a> {}; + %extra_argument &'a mut Program; %extra_token TokenMetadata; %error ErrorKind; %syntax_error { @@ -176,7 +177,13 @@ pomelo! { translation_unit ::= external_declaration; translation_unit ::= translation_unit external_declaration; - external_declaration ::= function_definition(f) { extra.functions.append(f); } + external_declaration ::= function_definition(f) { + let name = f.name.clone(); + let handle = extra.functions.append(f); + if let Some(name) = name { + extra.lookup_function.insert(name, handle); + } + } function_definition ::= function_prototype compound_statement_no_new_scope { Function {