Skip to content

Commit

Permalink
Added some structure for user types (struct at the moment).
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 23, 2009
1 parent 3050189 commit 066c5f7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
70 changes: 61 additions & 9 deletions mojoshader_compiler.c
@@ -1,18 +1,64 @@
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"

#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif

typedef struct TokenData
{
const char *token;
unsigned int tokenlen;
} TokenData;

typedef struct Context
{
Preprocessor *preprocessor;
const char *token; // assembler token!
unsigned int tokenlen; // assembler token!
Token tokenval; // assembler token!
const char *token;
unsigned int tokenlen;
Token tokenval;
unsigned int parse_errors;
TokenData usertypes[512]; // !!! FIXME: dynamic allocation
int usertype_count; // !!! FIXME: dynamic allocation
} Context;

#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif

static void add_usertype(Context *ctx, const char *token, unsigned int len)
{
// !!! FIXME: error if this is a reserved keyword.
// !!! FIXME: dynamic allocation
assert(ctx->usertype_count < STATICARRAYLEN(ctx->usertypes));
ctx->usertypes[ctx->usertype_count].token = token;
ctx->usertypes[ctx->usertype_count].tokenlen = len;
ctx->usertype_count++;
} // add_usertype

static int is_usertype(const Context *ctx)
{
// !!! FIXME: dynamic allocation
// !!! FIXME: should probably redesign this anyhow.
int i;
for (i = 0; i < ctx->usertype_count; i++)
{

printf(" xxx check '");
fwrite(ctx->token, ctx->tokenlen, 1, stdout);
printf("' vs '");
fwrite(ctx->usertypes[i].token, ctx->usertypes[i].tokenlen, 1, stdout);
printf("'\n");

if (ctx->usertypes[i].tokenlen == ctx->tokenlen)
{
if (memcmp(ctx->usertypes[i].token, ctx->token, ctx->tokenlen)==0)
return 1;
} // if
} // for

return 0;
} // is_usertype


// This is where the actual parsing happens. It's Lemon-generated!
#define __MOJOSHADER_HLSL_COMPILER__ 1
#include "mojoshader_parser_hlsl.h"

Expand Down Expand Up @@ -292,7 +338,8 @@ static int convert_to_lemon_token(const Context *ctx)

if (is_semantic(ctx))
return TOKEN_HLSL_SEMANTIC;

else if (is_usertype(ctx))
return TOKEN_HLSL_USERTYPE;
return TOKEN_HLSL_IDENTIFIER;

case TOKEN_EOI: return 0;
Expand All @@ -316,7 +363,8 @@ void MOJOSHADER_compile(const char *filename,
Context ctx;
if (m == NULL) m = MOJOSHADER_internal_malloc;
if (f == NULL) f = MOJOSHADER_internal_free;


memset(&ctx, '\0', sizeof (Context));
ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
include_open, include_close,
defines, define_count, 0, m, f, d);
Expand All @@ -331,8 +379,12 @@ void MOJOSHADER_compile(const char *filename,
ctx.token = preprocessor_nexttoken(ctx.preprocessor,
&ctx.tokenlen,
&ctx.tokenval);
ParseHLSL(pParser, convert_to_lemon_token(&ctx), 0, 0);

TokenData token = { ctx.token, ctx.tokenlen };
ParseHLSL(pParser, convert_to_lemon_token(&ctx), token, &ctx);
} while (ctx.tokenval != TOKEN_EOI);
ParseHLSLFree(pParser, f, d);
}

// end of mojoshader_compiler.c ...

16 changes: 11 additions & 5 deletions mojoshader_parser_hlsl.lemon
Expand Up @@ -21,7 +21,7 @@

%start_symbol shader
%token_prefix TOKEN_HLSL_
%token_type { IncludeState * }
%token_type { TokenData }
%extra_argument { Context *ctx }

%include {
Expand Down Expand Up @@ -161,7 +161,10 @@ variable_declaration_details ::= scalar_or_array.

// !!! FIXME: we don't handle full sampler declarations at the moment.

struct_declaration ::= STRUCT identifier LBRACE struct_member_list RBRACE.
struct_declaration ::= STRUCT identifier(A) LBRACE struct_member_list RBRACE.
{
add_usertype(ctx, A.token, A.tokenlen);
}

struct_member_list ::= struct_member.
struct_member_list ::= struct_member_list struct_member.
Expand Down Expand Up @@ -448,9 +451,12 @@ switch_case ::= CASE expression COLON.
switch_case ::= DEFAULT COLON statement_list.
switch_case ::= DEFAULT COLON.

// I may want to do more with this at some point.
identifier ::= IDENTIFIER.

%type identifier { TokenData }
identifier(A) ::= IDENTIFIER(B).
{
A.token = B.token;
A.tokenlen = B.tokenlen;
}

// the expression stuff is based on Jeff Lee's ANSI C grammar.
primary_expr ::= identifier.
Expand Down

0 comments on commit 066c5f7

Please sign in to comment.