From d26f75485304a574b4500a116794ee451e0c072c Mon Sep 17 00:00:00 2001 From: Pelle Johnsen Date: Tue, 14 Jul 2020 14:56:18 +0200 Subject: [PATCH] Add more glsl-new handling of funcs and types (#110) --- src/front/glsl_new/ast.rs | 6 +++- src/front/glsl_new/mod.rs | 1 + src/front/glsl_new/parser.rs | 63 +++++++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/src/front/glsl_new/ast.rs b/src/front/glsl_new/ast.rs index 61c1d18a1b..2ac0dd0bfe 100644 --- a/src/front/glsl_new/ast.rs +++ b/src/front/glsl_new/ast.rs @@ -1,4 +1,4 @@ -use crate::{Arena, FastHashMap, Function, Handle}; +use crate::{Arena, FastHashMap, Function, Handle, Type}; #[derive(Debug)] pub struct Program { @@ -7,6 +7,8 @@ pub struct Program { pub ext_decls: Vec, pub lookup_function: FastHashMap>, pub functions: Arena, + pub lookup_type: FastHashMap>, + pub types: Arena, } impl Program { @@ -17,6 +19,8 @@ impl Program { ext_decls: vec![], lookup_function: FastHashMap::default(), functions: Arena::::new(), + lookup_type: FastHashMap::default(), + types: Arena::::new(), } } } diff --git a/src/front/glsl_new/mod.rs b/src/front/glsl_new/mod.rs index 4836f5efb1..ca5c530f2a 100644 --- a/src/front/glsl_new/mod.rs +++ b/src/front/glsl_new/mod.rs @@ -29,6 +29,7 @@ pub fn parse_str(source: &str, entry: String, stage: ShaderStage) -> Result {}; @@ -38,8 +39,18 @@ pomelo! { %type DoubleConstant f64; %type String String; %type arg_list Vec; + + %type function_prototype Function; + %type function_declarator Function; + %type function_header Function; + %type function_definition Function; + %type fully_specified_type Option>; + %type type_specifier Option>; + %type type_specifier_nonarray Option; + + root ::= version_pragma translation_unit; version_pragma ::= Version IntConstant(V) Identifier?(P) { match V.1 { @@ -178,16 +189,44 @@ pomelo! { // function - function_prototype ::= function_declarator RightParen; + function_prototype ::= function_declarator(f) RightParen {f} function_declarator ::= function_header; - function_header ::= fully_specified_type Identifier LeftParen; + function_header ::= fully_specified_type(t) Identifier(n) LeftParen { + Function { + name: Some(n.1), + parameter_types: vec![], + return_type: t, + global_usage: vec![], + local_variables: Arena::::new(), + expressions: Arena::::new(), + body: vec![], + } + } // type fully_specified_type ::= type_specifier; - type_specifier ::= type_specifier_nonarray; + type_specifier ::= type_specifier_nonarray(t) { + t.map(|t| { + let name = t.name.clone(); + let handle = extra.types.fetch_or_append(t); + if let Some(name) = name { + extra.lookup_type.insert(name, handle); + } + handle + }) + } - type_specifier_nonarray ::= Void; - type_specifier_nonarray ::= Vec4; + type_specifier_nonarray ::= Void { None } + type_specifier_nonarray ::= Vec4 { + Some(Type { + name: None, + inner: TypeInner::Vector { + size: VectorSize::Quad, + kind: ScalarKind::Float, + width: 16, + } + }) + } //TODO: remaining types // misc @@ -202,16 +241,8 @@ pomelo! { } } - function_definition ::= function_prototype compound_statement_no_new_scope { - Function { - name: Some(String::from("main")), - parameter_types: vec![], - return_type: None, - global_usage: vec![], - local_variables: Arena::::new(), - expressions: Arena::::new(), - body: vec![], - } + function_definition ::= function_prototype(f) compound_statement_no_new_scope { + f }; }