Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip preprocessor directives #13

Merged
merged 3 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 27 additions & 14 deletions lslmini.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const char *DEPRECATED_FUNCTIONS[][2] = {

int walklevel = 0;
int mono_mode = 1;
int skip_preproc = 0;

void print_walk( char *str ) {
int i;
Expand Down Expand Up @@ -158,12 +159,12 @@ void LLASTNode::define_symbol(LLScriptSymbol *symbol) {
}
}

// Otherwise, ask our parent to define it
// Otherwise, ask our parent to define it
} else if ( parent ) {

parent->define_symbol(symbol);

// .. but if we don't have a parent, we're in trouble.
// .. but if we don't have a parent, we're in trouble.
} else {

throw "nowhere to define symbol!";
Expand Down Expand Up @@ -197,7 +198,7 @@ void LLScriptGlobalVariable::define_symbols() {
define_symbol(identifier->get_symbol());

// if it's initialized, set it's constant value
if ( get_child(1)->get_node_type() == NODE_SIMPLE_ASSIGNABLE )
if ( get_child(1)->get_node_type() == NODE_SIMPLE_ASSIGNABLE )
identifier->get_symbol()->set_constant_value( get_child(1)->get_child(0)->get_constant_value() );
}

Expand All @@ -213,7 +214,7 @@ void LLScriptState::define_symbols() {
return;

identifier = (LLScriptIdentifier *)node;
identifier->set_symbol( new LLScriptSymbol(identifier->get_name(), identifier->get_type(), SYM_STATE, SYM_GLOBAL, identifier->get_lloc()) );
identifier->set_symbol( new LLScriptSymbol(identifier->get_name(), identifier->get_type(), SYM_STATE, SYM_GLOBAL, identifier->get_lloc()) );
define_symbol( identifier->get_symbol() );
}

Expand Down Expand Up @@ -294,7 +295,7 @@ static char* operation_str(int operation) {
case BOOLEAN_OR: return "||";
case SHIFT_LEFT: return "<<";
case SHIFT_RIGHT: return ">>";
default:
default:
if ( isprint(operation) ) {
buf[0] = operation;
buf[1] = 0;
Expand All @@ -310,7 +311,7 @@ void LLScriptExpression::determine_type() {
else {
type = get_child(0)->get_type()->get_result_type( operation, get_child(1) ? get_child(1)->get_type() : NULL );
if ( type == NULL ) {
ERROR( HERE, E_INVALID_OPERATOR, get_child(0)->get_type()->get_node_name(), operation_str(operation), get_child(1) ? get_child(1)->get_type()->get_node_name() : "" );
ERROR( HERE, E_INVALID_OPERATOR, get_child(0)->get_type()->get_node_name(), operation_str(operation), get_child(1) ? get_child(1)->get_type()->get_node_name() : "" );
type = get_child(0)->get_type();
} else {
if ( operation == '=' || operation == INC_OP || operation == DEC_OP ) {
Expand Down Expand Up @@ -739,16 +740,18 @@ void LLASTNode::check_symbols() {
void usage(char *name) {
printf("Usage: %s [options] [input]\n", name);
printf("Options: \n");
printf("\t-m\t\t\tUse Mono rules for the analysis (default).\n");
printf("\t-M\t\t\tUse LSO rules for the analysis.\n");
printf("\t-b <file>\tLoad builtin functions from file.\n");
printf("\t-m\t\tUse Mono rules for the analysis (default)\n");
printf("\t-m-\t\tUse LSO rules for the analysis\n");
printf("\t-b <file>\tLoad builtin functions from file\n");
printf("\t-t\t\tShow tree structure.\n");
printf("\t-l\t\tShow line/column information as range\n");
printf("\t-p\t\tAdd the file path to the result\n");
printf("\t-v\t\tBe verbose\n");
printf("\t-S\t\tDon't sort log messages\n");
printf("\t-#\t\tShow error codes (for debugging/testing)\n");
printf("\t-A\t\tCheck error assertions (for debugging/testing)\n");
printf("\t-i\t\tTreat preprocessor directives as comments\n");
printf("\t-i-\t\tDon't handle preproc. directives specially (default)\n");
#ifdef COMPILE_ENABLED
printf("\t-c\t\tCompile.\t\t\t(default)\n");
printf("\t-C\t\tDon't compile.\n");
Expand Down Expand Up @@ -807,16 +810,20 @@ int main(int argc, char **argv) {
void *scanner;
Logger *logger = Logger::get();

#ifdef COMPILE_ENABLED
#ifdef COMPILE_ENABLED
bool compile = true;
#endif

for ( i = 1; i < argc; ++i ) {
if ( argv[i][0] == '-' ) {
for ( j = 1 ; argv[i][j]; ++j ) {
switch( argv[i][j] ) {
case 'm': mono_mode = 1; break;
case 'M': mono_mode = 0; break;
case 'm':
if (argv[i][j+1] == '-') {
mono_mode = 0;
j++;
} else mono_mode = 1;
break;
case 'b': builtins_file = argv[++i]; goto nextarg;
case 't': show_tree = true; break;
case 'l': logger->set_show_end(true); break;
Expand All @@ -826,7 +833,13 @@ int main(int argc, char **argv) {
case 'A': logger->set_check_assertions(true); break;
case 'p': print_path = true; break;
case 'V': version(); return 0;
#ifdef COMPILE_ENABLED
case 'i':
if (argv[i][j+1] == '-') {
skip_preproc = 0;
j++;
} else skip_preproc = 1;
break;
#ifdef COMPILE_ENABLED
case 'c': compile = true; break;
case 'C': compile = false; break;
#endif /* COMPILE_ENABLED */
Expand Down Expand Up @@ -886,7 +899,7 @@ int main(int argc, char **argv) {

script->generate_cil();
}
#endif /* COMPILE_ENABLED */
#endif /* COMPILE_ENABLED */
} else {
Logger::get()->report();
}
Expand Down
1 change: 1 addition & 0 deletions lslmini.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef float F32;
extern class LLScriptScript *script;
extern int walklevel;
extern int mono_mode;
extern int skip_preproc;
void print_walk(char *str);
void do_walk(class LLASTNode *node);

Expand Down
30 changes: 17 additions & 13 deletions lslmini.l
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ char *parse_string(char *input);
%}


D [0-9]
BD [-]?[0-9]
N [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
Expand All @@ -36,6 +34,8 @@ FS (f|F)
/* exclusive state to eat comments of any length without overflowing any buffers */
%x COMMENT
%x C_COMMENT
/* exclusive state to skip preprocessor commands */
%x PREPROC

%%
%{
Expand All @@ -48,14 +48,14 @@ FS (f|F)
ErrorCode e = (ErrorCode) strtoul( yytext+3, NULL, 10 );
LOG( LOG_INFO, yylloc, "Adding assertion for E%d.", (int)e );
Logger::get()->add_assertion( yylloc->first_line, e );
}
<COMMENT>. { /* eat comments */ }
<COMMENT>\n { BEGIN 0; LLOC_LINES(1); LLOC_STEP(); }
}
<COMMENT>. { /* eat comments */ }
<COMMENT>\n { BEGIN 0; LLOC_LINES(1); LLOC_STEP(); }

"/*" { BEGIN C_COMMENT; }
<C_COMMENT>"*/" { BEGIN 0; LLOC_STEP(); }
<C_COMMENT>\n { LLOC_LINES(1); LLOC_STEP(); }
<C_COMMENT>. { LLOC_STEP(); }
"/*" { BEGIN C_COMMENT; }
<C_COMMENT>"*/" { BEGIN 0; LLOC_STEP(); }
<C_COMMENT>\n { LLOC_LINES(1); LLOC_STEP(); }
<C_COMMENT>. { LLOC_STEP(); }

"integer" { return(INTEGER); }
"float" { return(FLOAT_TYPE); }
Expand All @@ -77,18 +77,18 @@ FS (f|F)
"do" { return(DO); }
"while" { return(WHILE); }

"." { return(PERIOD); }
"." { return(PERIOD); }


0[xX]{H}+ { yylval->ival = strtoul(yytext, NULL, 0); return(INTEGER_CONSTANT); }
{N}+ { yylval->ival = strtoul(yytext, NULL, 10); return(INTEGER_CONSTANT); }
{N}+ { yylval->ival = strtoul(yytext, NULL, 10); return(INTEGER_CONSTANT); }

"TRUE" { yylval->ival = 1; return(INTEGER_TRUE); }
"FALSE" { yylval->ival = 0; return(INTEGER_FALSE); }

{L}({L}|{N})* { yylval->sval = new char[strlen(yytext) + 1]; strcpy(yylval->sval, yytext); return(IDENTIFIER); }

{N}+{E} { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
{N}+{E} { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
{N}*"."{N}+({E})?{FS}? { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }
{N}+"."{N}*({E})?{FS}? { yylval->fval = (F32)atof(yytext); return(FP_CONSTANT); }

Expand Down Expand Up @@ -133,8 +133,12 @@ L?\"(\\.|[^\\"])*\" { yylval->sval = parse_string(yytext); return(STRING_CONSTAN
"<<" { return(SHIFT_LEFT); }
">>" { return(SHIFT_RIGHT); }

^[ \t\f\v\r]*"#" { LLOC_STEP(); if (skip_preproc) { BEGIN PREPROC; } }
<PREPROC>[^\n]*\\\r?\n { LLOC_LINES(1); LLOC_STEP(); }
<PREPROC>[^\n]*\n { BEGIN 0; LLOC_LINES(1); LLOC_STEP(); }


\n { LLOC_LINES(1); LLOC_STEP(); }
\n { LLOC_LINES(1); LLOC_STEP(); }
. { LLOC_STEP(); /* ignore bad characters */ }

%%
Expand Down