mojoshader_compiler.c
changeset 706 01a92f30b84f
child 709 6fbd0e20b40f
equal deleted inserted replaced
705:7ea12a852213 706:01a92f30b84f
       
     1 typedef struct Context Context;
       
     2 #include "mojoshader_parser_hlsl.h"
       
     3 #include "mojoshader_parser_hlsl.c"
       
     4 
       
     5 struct Context
       
     6 {
       
     7     Preprocessor *preprocessor;
       
     8     const char *token;      // assembler token!
       
     9     unsigned int tokenlen;  // assembler token!
       
    10     Token tokenval;         // assembler token!
       
    11 };
       
    12 
       
    13 static int ConvertToLemonToken(const Context *ctx)
       
    14 {
       
    15     switch (ctx->tokenval)
       
    16     {
       
    17         case ((Token) ','): return TOKEN_HLSL_COMMA;
       
    18         case ((Token) '='): return TOKEN_HLSL_ASSIGN;
       
    19         case ((Token) TOKEN_ADDASSIGN): return TOKEN_HLSL_ADDASSIGN;
       
    20         case ((Token) TOKEN_SUBASSIGN): return TOKEN_HLSL_SUBASSIGN;
       
    21         case ((Token) TOKEN_MULTASSIGN): return TOKEN_HLSL_MULASSIGN;
       
    22         case ((Token) TOKEN_DIVASSIGN): return TOKEN_HLSL_DIVASSIGN;
       
    23         case ((Token) TOKEN_MODASSIGN): return TOKEN_HLSL_MODASSIGN;
       
    24         case ((Token) TOKEN_LSHIFTASSIGN): return TOKEN_HLSL_LSHIFTASSIGN;
       
    25         case ((Token) TOKEN_RSHIFTASSIGN): return TOKEN_HLSL_RSHIFTASSIGN;
       
    26         case ((Token) TOKEN_ANDASSIGN): return TOKEN_HLSL_ANDASSIGN;
       
    27         case ((Token) TOKEN_ORASSIGN): return TOKEN_HLSL_ORASSIGN;
       
    28         case ((Token) TOKEN_XORASSIGN): return TOKEN_HLSL_XORASSIGN;
       
    29         case ((Token) '?'): return TOKEN_HLSL_QUESTION;
       
    30         case ((Token) TOKEN_OROR): return TOKEN_HLSL_OROR;
       
    31         case ((Token) TOKEN_ANDAND): return TOKEN_HLSL_ANDAND;
       
    32         case ((Token) '|'): return TOKEN_HLSL_OR;
       
    33         case ((Token) '^'): return TOKEN_HLSL_XOR;
       
    34         case ((Token) '&'): return TOKEN_HLSL_AND;
       
    35         case ((Token) TOKEN_EQL): return TOKEN_HLSL_EQL;
       
    36         case ((Token) TOKEN_NEQ): return TOKEN_HLSL_NEQ;
       
    37         case ((Token) '<'): return TOKEN_HLSL_LT;
       
    38         case ((Token) TOKEN_LEQ): return TOKEN_HLSL_LEQ;
       
    39         case ((Token) '>'): return TOKEN_HLSL_GT;
       
    40         case ((Token) TOKEN_GEQ): return TOKEN_HLSL_GEQ;
       
    41         case ((Token) TOKEN_LSHIFT): return TOKEN_HLSL_LSHIFT;
       
    42         case ((Token) TOKEN_RSHIFT): return TOKEN_HLSL_RSHIFT;
       
    43         case ((Token) '+'): return TOKEN_HLSL_PLUS;
       
    44         case ((Token) '-'): return TOKEN_HLSL_MINUS;
       
    45         case ((Token) '*'): return TOKEN_HLSL_STAR;
       
    46         case ((Token) '/'): return TOKEN_HLSL_SLASH;
       
    47         case ((Token) '%'): return TOKEN_HLSL_PERCENT;
       
    48         case ((Token) '!'): return TOKEN_HLSL_EXCLAMATION;
       
    49         case ((Token) '~'): return TOKEN_HLSL_COMPLEMENT;
       
    50         case ((Token) TOKEN_DECREMENT): return TOKEN_HLSL_MINUSMINUS;
       
    51         case ((Token) TOKEN_INCREMENT): return TOKEN_HLSL_PLUSPLUS;
       
    52         case ((Token) '.'): return TOKEN_HLSL_DOT;
       
    53         case ((Token) '['): return TOKEN_HLSL_LBRACKET;
       
    54         case ((Token) ']'): return TOKEN_HLSL_RBRACKET;
       
    55         case ((Token) '('): return TOKEN_HLSL_LPAREN;
       
    56         case ((Token) ')'): return TOKEN_HLSL_RPAREN;
       
    57         case ((Token) TOKEN_INT_LITERAL): return TOKEN_HLSL_CONSTANT;
       
    58         case ((Token) TOKEN_FLOAT_LITERAL): return TOKEN_HLSL_CONSTANT;
       
    59         case ((Token) TOKEN_STRING_LITERAL): return TOKEN_HLSL_STRING_LITERAL;
       
    60         case ((Token) ':'): return TOKEN_HLSL_COLON;
       
    61         case ((Token) ';'): return TOKEN_HLSL_SEMICOLON;
       
    62         case ((Token) '{'): return TOKEN_HLSL_LBRACE;
       
    63         case ((Token) '}'): return TOKEN_HLSL_RBRACE;
       
    64 
       
    65         case ((Token) TOKEN_IDENTIFIER):
       
    66             #define tokencmp(t) ((ctx->tokenlen == strlen(t)) && (memcmp(ctx->token, t, ctx->tokenlen) == 0))
       
    67             //case ((Token) ''): return TOKEN_HLSL_TYPECAST
       
    68             //if (tokencmp("")) return TOKEN_HLSL_TYPE_NAME
       
    69             //if (tokencmp("...")) return TOKEN_HLSL_ELIPSIS
       
    70             if (tokencmp("else")) return TOKEN_HLSL_ELSE;
       
    71             if (tokencmp("sizeof")) return TOKEN_HLSL_SIZEOF;
       
    72             if (tokencmp("typedef")) return TOKEN_HLSL_TYPEDEF;
       
    73             if (tokencmp("extern")) return TOKEN_HLSL_EXTERN;
       
    74             if (tokencmp("static")) return TOKEN_HLSL_STATIC;
       
    75             if (tokencmp("auto")) return TOKEN_HLSL_AUTO;
       
    76             if (tokencmp("register")) return TOKEN_HLSL_REGISTER;
       
    77             if (tokencmp("char")) return TOKEN_HLSL_CHAR;
       
    78             if (tokencmp("short")) return TOKEN_HLSL_SHORT;
       
    79             if (tokencmp("int")) return TOKEN_HLSL_INT;
       
    80             if (tokencmp("long")) return TOKEN_HLSL_LONG;
       
    81             if (tokencmp("signed")) return TOKEN_HLSL_SIGNED;
       
    82             if (tokencmp("unsigned")) return TOKEN_HLSL_UNSIGNED;
       
    83             if (tokencmp("float")) return TOKEN_HLSL_FLOAT;
       
    84             if (tokencmp("double")) return TOKEN_HLSL_DOUBLE;
       
    85             if (tokencmp("const")) return TOKEN_HLSL_CONST;
       
    86             if (tokencmp("volatile")) return TOKEN_HLSL_VOLATILE;
       
    87             if (tokencmp("void")) return TOKEN_HLSL_VOID;
       
    88             if (tokencmp("struct")) return TOKEN_HLSL_STRUCT;
       
    89             if (tokencmp("union")) return TOKEN_HLSL_UNION;
       
    90             if (tokencmp("enum")) return TOKEN_HLSL_ENUM;
       
    91             if (tokencmp("case")) return TOKEN_HLSL_CASE;
       
    92             if (tokencmp("default")) return TOKEN_HLSL_DEFAULT;
       
    93             if (tokencmp("if")) return TOKEN_HLSL_IF;
       
    94             if (tokencmp("switch")) return TOKEN_HLSL_SWITCH;
       
    95             if (tokencmp("while")) return TOKEN_HLSL_WHILE;
       
    96             if (tokencmp("do")) return TOKEN_HLSL_DO;
       
    97             if (tokencmp("for")) return TOKEN_HLSL_FOR;
       
    98             if (tokencmp("goto")) return TOKEN_HLSL_GOTO;
       
    99             if (tokencmp("continue")) return TOKEN_HLSL_CONTINUE;
       
   100             if (tokencmp("break")) return TOKEN_HLSL_BREAK;
       
   101             if (tokencmp("return")) return TOKEN_HLSL_RETURN;
       
   102             #undef tokencmp
       
   103             return TOKEN_HLSL_IDENTIFIER;
       
   104 
       
   105         case TOKEN_EOI: return 0;
       
   106         case TOKEN_BAD_CHARS: printf("bad chars from lexer\n"); return 0;
       
   107         case TOKEN_PREPROCESSING_ERROR: printf("error from lexer\n"); return 0;
       
   108         default: assert(0 && "unexpected token from lexer\n"); return 0;
       
   109     } // switch
       
   110 
       
   111     return 0;
       
   112 }
       
   113 
       
   114 
       
   115 void MOJOSHADER_compile(const char *filename,
       
   116                              const char *source, unsigned int sourcelen,
       
   117                              const MOJOSHADER_preprocessorDefine *defines,
       
   118                              unsigned int define_count,
       
   119                              MOJOSHADER_includeOpen include_open,
       
   120                              MOJOSHADER_includeClose include_close,
       
   121                              MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
       
   122 {
       
   123     Context ctx;
       
   124     if (m == NULL) m = MOJOSHADER_internal_malloc;
       
   125     if (f == NULL) f = MOJOSHADER_internal_free;
       
   126     
       
   127     ctx.preprocessor = preprocessor_start(filename, source, sourcelen,
       
   128                                            include_open, include_close,
       
   129                                            defines, define_count, 0, m, f, d);
       
   130 
       
   131     void *pParser = ParseHLSLAlloc(m, d);
       
   132     ParseHLSLTrace(stdout, "TRACE: ");
       
   133 
       
   134     do {
       
   135         ctx.token = preprocessor_nexttoken(ctx.preprocessor,
       
   136                                                 &ctx.tokenlen,
       
   137                                                 &ctx.tokenval);
       
   138         ParseHLSL(pParser, ConvertToLemonToken(&ctx), 0, 0);
       
   139     } while (ctx.tokenval != TOKEN_EOI);
       
   140     ParseHLSLFree(pParser, f, d);
       
   141 }
       
   142