Add inital ast for glsl-new (#107)

* Add inital ast for glsl-new

* Fix clippy for glsl-new

Deref instead of cloning
This commit is contained in:
Pelle Johnsen
2020-07-08 14:46:31 +02:00
committed by GitHub
parent 28f22810a0
commit c9fea37430
3 changed files with 59 additions and 29 deletions

34
src/front/glsl_new/ast.rs Normal file
View File

@@ -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<ExtDecl>,
pub lookup_function: FastHashMap<String, Handle<Function>>,
pub functions: Arena<Function>,
}
impl Program {
pub fn new() -> Program {
Program {
version: 0,
profile: Profile::Core,
ext_decls: vec![],
lookup_function: FastHashMap::default(),
functions: Arena::<Function>::new(),
}
}
}
#[derive(Debug)]
pub enum Profile {
// Compatibility,
Core,
// Es,
}
#[derive(Debug)]
pub enum ExtDecl {
// FunctionDecl,
}

View File

@@ -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<Module, ParseError> {
log::debug!("------ GLSL-pomelo ------");
let module = Module {
header: Header {
version: (1, 0, 0),
generator: 0,
},
types: Arena::<Type>::new(),
constants: Arena::<Constant>::new(),
global_variables: Arena::<GlobalVariable>::new(),
functions: Arena::<Function>::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)
}

View File

@@ -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 {