2009-06-20 16:10:55 +04:00
|
|
|
/*
|
|
|
|
* OpenSCAD (www.openscad.at)
|
|
|
|
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
%{
|
|
|
|
|
|
|
|
#include "openscad.h"
|
|
|
|
|
|
|
|
int parserlex(void);
|
|
|
|
void yyerror(char const *s);
|
|
|
|
|
|
|
|
int lexerget_lineno(void);
|
2009-06-21 12:59:53 +04:00
|
|
|
int lexerlex_destroy(void);
|
2009-06-20 16:10:55 +04:00
|
|
|
int lexerlex(void);
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QVector<Module*> module_stack;
|
|
|
|
Module *module;
|
|
|
|
|
2009-06-21 12:59:53 +04:00
|
|
|
class ArgContainer {
|
|
|
|
public:
|
|
|
|
QString argname;
|
|
|
|
Expression *argexpr;
|
|
|
|
};
|
|
|
|
class ArgsContainer {
|
|
|
|
public:
|
|
|
|
QVector<QString> argnames;
|
|
|
|
QVector<Expression*> argexpr;
|
|
|
|
};
|
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
%}
|
|
|
|
|
|
|
|
%union {
|
|
|
|
char *text;
|
|
|
|
double number;
|
2009-06-21 12:59:53 +04:00
|
|
|
class Expression *expr;
|
|
|
|
class ModuleInstanciation *inst;
|
|
|
|
class ArgContainer *arg;
|
|
|
|
class ArgsContainer *args;
|
2009-06-20 16:10:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
%token TOK_MODULE
|
|
|
|
%token TOK_FUNCTION
|
|
|
|
%token <text> TOK_ID
|
|
|
|
%token <text> TOK_STRING
|
|
|
|
%token <number> TOK_NUMBER
|
|
|
|
|
|
|
|
%left '+' '-'
|
|
|
|
%left '*' '/' '%'
|
|
|
|
%left '.'
|
|
|
|
|
2009-06-21 12:59:53 +04:00
|
|
|
%type <expr> expr
|
|
|
|
|
|
|
|
%type <inst> module_instantciation
|
|
|
|
%type <inst> module_instantciation_list
|
|
|
|
%type <inst> single_module_instantciation
|
|
|
|
|
|
|
|
%type <args> arguments_call
|
|
|
|
%type <args> arguments_decl
|
|
|
|
|
|
|
|
%type <arg> argument_call
|
|
|
|
%type <arg> argument_decl
|
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
%debug
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
input:
|
|
|
|
/* empty */ |
|
|
|
|
statement input ;
|
|
|
|
|
|
|
|
statement:
|
|
|
|
';' |
|
|
|
|
'{' input '}' |
|
2009-06-21 12:59:53 +04:00
|
|
|
module_instantciation {
|
|
|
|
module->children.append($1);
|
|
|
|
} |
|
|
|
|
TOK_ID '=' expr ';' {
|
|
|
|
module->assignments[$1] = $3;
|
|
|
|
free($1);
|
|
|
|
} |
|
|
|
|
TOK_MODULE TOK_ID '(' arguments_decl ')' {
|
|
|
|
Module *p = module;
|
|
|
|
module_stack.append(module);
|
|
|
|
module = new Module();
|
|
|
|
p->modules[$2] = module;
|
|
|
|
module->argnames = $4->argnames;
|
|
|
|
module->argexpr = $4->argexpr;
|
|
|
|
free($2);
|
|
|
|
delete $4;
|
|
|
|
} statement {
|
|
|
|
module = module_stack.last();
|
|
|
|
module_stack.pop_back();
|
|
|
|
} |
|
|
|
|
TOK_FUNCTION TOK_ID '(' arguments_decl ')' '=' expr {
|
|
|
|
Function *func = new Function();
|
|
|
|
func->argnames = $4->argnames;
|
|
|
|
func->argexpr = $4->argexpr;
|
|
|
|
func->expr = $7;
|
|
|
|
module->functions[$2] = func;
|
|
|
|
free($2);
|
|
|
|
delete $4;
|
|
|
|
} ;
|
2009-06-20 16:10:55 +04:00
|
|
|
|
2009-06-21 12:59:53 +04:00
|
|
|
module_instantciation:
|
|
|
|
single_module_instantciation ';' {
|
|
|
|
$$ = $1;
|
|
|
|
} |
|
|
|
|
single_module_instantciation '{' module_instantciation_list '}' {
|
|
|
|
$$ = $1;
|
|
|
|
$$->children = $3->children;
|
|
|
|
$3->children.clear();
|
|
|
|
delete $3;
|
|
|
|
} |
|
|
|
|
single_module_instantciation module_instantciation {
|
|
|
|
$$ = $1;
|
|
|
|
$$->children.append($2);
|
|
|
|
} ;
|
2009-06-20 16:10:55 +04:00
|
|
|
|
2009-06-21 12:59:53 +04:00
|
|
|
module_instantciation_list:
|
|
|
|
/* empty */ {
|
|
|
|
$$ = new ModuleInstanciation();
|
|
|
|
} |
|
|
|
|
module_instantciation module_instantciation_list {
|
|
|
|
$$ = $2;
|
|
|
|
$$->children.append($1);
|
|
|
|
} ;
|
|
|
|
|
|
|
|
single_module_instantciation:
|
|
|
|
TOK_ID '(' arguments_call ')' {
|
|
|
|
$$ = new ModuleInstanciation();
|
|
|
|
$$->modname = QString($1);
|
|
|
|
$$->argnames = $3->argnames;
|
|
|
|
$$->argexpr = $3->argexpr;
|
|
|
|
free($1);
|
|
|
|
delete $3;
|
|
|
|
} |
|
|
|
|
TOK_ID ':' TOK_ID '(' arguments_call ')' {
|
|
|
|
$$ = new ModuleInstanciation();
|
|
|
|
$$->label = QString($1);
|
|
|
|
$$->modname = QString($3);
|
|
|
|
$$->argnames = $5->argnames;
|
|
|
|
$$->argexpr = $5->argexpr;
|
|
|
|
free($1);
|
|
|
|
free($3);
|
|
|
|
delete $5;
|
|
|
|
} ;
|
|
|
|
|
2009-06-20 16:10:55 +04:00
|
|
|
expr:
|
2009-06-21 12:59:53 +04:00
|
|
|
TOK_ID {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'L';
|
|
|
|
$$->var_name = QString($1);
|
|
|
|
free($1);
|
|
|
|
} |
|
|
|
|
expr '.' TOK_ID {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'M';
|
|
|
|
$$->children.append($1);
|
|
|
|
$$->var_name = QString($3);
|
|
|
|
free($3);
|
|
|
|
} |
|
|
|
|
TOK_STRING {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'C';
|
|
|
|
$$->const_value = Value(QString($1));
|
|
|
|
free($1);
|
|
|
|
} |
|
|
|
|
TOK_NUMBER {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'C';
|
|
|
|
$$->const_value = Value($1);
|
|
|
|
} |
|
|
|
|
TOK_NUMBER ':' TOK_NUMBER {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'C';
|
|
|
|
$$->const_value = Value($1, 1, $3);
|
|
|
|
$$->const_value.is_range = true;
|
|
|
|
} |
|
|
|
|
TOK_NUMBER ':' TOK_NUMBER ':' TOK_NUMBER {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'C';
|
|
|
|
$$->const_value = Value($1, $3, $5);
|
|
|
|
$$->const_value.is_range = true;
|
|
|
|
} |
|
|
|
|
'[' TOK_NUMBER TOK_NUMBER TOK_NUMBER ']' {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'C';
|
|
|
|
$$->const_value = Value($2, $3, $4);
|
|
|
|
} |
|
|
|
|
'[' expr ',' expr ',' expr ']' {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'V';
|
|
|
|
$$->children.append($2);
|
|
|
|
$$->children.append($4);
|
|
|
|
$$->children.append($6);
|
|
|
|
} |
|
|
|
|
expr '*' expr {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = '*';
|
|
|
|
$$->children.append($1);
|
|
|
|
$$->children.append($3);
|
|
|
|
} |
|
|
|
|
expr '/' expr {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = '/';
|
|
|
|
$$->children.append($1);
|
|
|
|
$$->children.append($3);
|
|
|
|
} |
|
|
|
|
expr '%' expr {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = '%';
|
|
|
|
$$->children.append($1);
|
|
|
|
$$->children.append($3);
|
|
|
|
} |
|
|
|
|
expr '+' expr {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = '+';
|
|
|
|
$$->children.append($1);
|
|
|
|
$$->children.append($3);
|
|
|
|
} |
|
|
|
|
expr '-' expr {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = '-';
|
|
|
|
$$->children.append($1);
|
|
|
|
$$->children.append($3);
|
|
|
|
} |
|
|
|
|
'+' expr {
|
|
|
|
$$ = $2;
|
|
|
|
} |
|
|
|
|
'-' expr {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'I';
|
|
|
|
$$->children.append($2);
|
|
|
|
} |
|
|
|
|
'(' expr ')' {
|
|
|
|
$$ = $2;
|
|
|
|
} |
|
|
|
|
TOK_ID '(' arguments_call ')' {
|
|
|
|
$$ = new Expression();
|
|
|
|
$$->type = 'F';
|
|
|
|
$$->call_funcname = QString($1);
|
|
|
|
$$->call_argnames = $3->argnames;
|
|
|
|
$$->children = $3->argexpr;
|
|
|
|
free($1);
|
|
|
|
delete $3;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
arguments_decl:
|
|
|
|
/* empty */ {
|
|
|
|
$$ = new ArgsContainer();
|
|
|
|
} |
|
|
|
|
argument_decl {
|
|
|
|
$$ = new ArgsContainer();
|
|
|
|
$$->argnames.append($1->argname);
|
|
|
|
$$->argexpr.append($1->argexpr);
|
|
|
|
delete $1;
|
|
|
|
} |
|
|
|
|
arguments_decl ',' argument_decl {
|
|
|
|
$$ = $1;
|
|
|
|
$$->argnames.append($3->argname);
|
|
|
|
$$->argexpr.append($3->argexpr);
|
|
|
|
delete $3;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
argument_decl:
|
|
|
|
TOK_ID {
|
|
|
|
$$ = new ArgContainer();
|
|
|
|
$$->argname = QString($1);
|
|
|
|
$$->argexpr = NULL;
|
|
|
|
free($1);
|
|
|
|
} |
|
|
|
|
TOK_ID '=' expr {
|
|
|
|
$$ = new ArgContainer();
|
|
|
|
$$->argname = QString($1);
|
|
|
|
$$->argexpr = $3;
|
|
|
|
free($1);
|
|
|
|
} ;
|
2009-06-20 16:10:55 +04:00
|
|
|
|
|
|
|
arguments_call:
|
2009-06-21 12:59:53 +04:00
|
|
|
/* empty */ {
|
|
|
|
$$ = new ArgsContainer();
|
|
|
|
} |
|
|
|
|
argument_call {
|
|
|
|
$$ = new ArgsContainer();
|
|
|
|
$$->argnames.append($1->argname);
|
|
|
|
$$->argexpr.append($1->argexpr);
|
|
|
|
delete $1;
|
|
|
|
} |
|
|
|
|
arguments_call ',' argument_call {
|
|
|
|
$$ = $1;
|
|
|
|
$$->argnames.append($3->argname);
|
|
|
|
$$->argexpr.append($3->argexpr);
|
|
|
|
delete $3;
|
|
|
|
} ;
|
2009-06-20 16:10:55 +04:00
|
|
|
|
|
|
|
argument_call:
|
2009-06-21 12:59:53 +04:00
|
|
|
expr {
|
|
|
|
$$ = new ArgContainer();
|
|
|
|
$$->argexpr = $1;
|
|
|
|
} |
|
|
|
|
TOK_ID '=' expr {
|
|
|
|
$$ = new ArgContainer();
|
|
|
|
$$->argname = QString($1);
|
|
|
|
$$->argexpr = $3;
|
|
|
|
free($1);
|
|
|
|
} ;
|
2009-06-20 16:10:55 +04:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
int parserlex(void)
|
|
|
|
{
|
|
|
|
return lexerlex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void yyerror (char const *s)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Parser error in line %d: %s\n", lexerget_lineno(), s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
AbstractModule *parse(FILE *f, int debug)
|
|
|
|
{
|
|
|
|
module_stack.clear();
|
|
|
|
module = new Module();
|
|
|
|
|
|
|
|
parserdebug = debug;
|
|
|
|
parserparse();
|
|
|
|
|
2009-06-21 12:59:53 +04:00
|
|
|
lexerlex_destroy();
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|