return ret;
}
-AST* DLParser::parseMacroParam(Param* param)
-{
- AST* ret = NULL;
- switch( param->type() )
- {
- case ExpTyp:
- ret = LogicalExpr();
- break;
-
- case BlockTyp:
- ret = FuncLiteral();
- break;
-
- default:
- Token& tok = lookaheadToken(1);
- ostringstream oss;
- oss << "Expected macro parameter type. Expected " << param->type() << ", received " << tok.type() << ".";
- Exception ex( tok.line(), tok.column() );
- ex.setMessage(oss.str());
- throw ex;
- break;
- }
- return ret;
-}
-
bool DLParser::speculate_GroupExpr(void)
{
AST* throw_away = NULL;
return _new AST(MACRO);
}
-AST* DLParser::MacroExpansion(void)
-{
- AST* ret = NULL;
- Macro* cur_macro = macros[ lookaheadToken(1).text() ];
- list<Param*>::const_iterator it = cur_macro->params().begin();
-
- consume();
- for(; it != cur_macro->params().end(); it++)
- {
- (*it)->setValue( parseMacroParam( *it ) );
- }
- ret = cur_macro->apply();
-
- return ret;
-}
-
// MacroPatternList = MacroPattern (',' MacroPattern)*
AST* DLParser::MacroPatternList(void)
{
match(SEP);
ret = _new AST(PATT, 2, ret, LogicalExpr());
- //AST* ret = _new AST( ID, lookaheadToken(1).text() );
- //consume();
- //if( lookaheadType(1) == SEP )
- //{
- // match(SEP);
- // AST* type = _new AST( ID, lookaheadToken(1).text() );
- // consume();
- // ret = _new AST(SEP, 2, ret, type);
- //}
return ret;
}
#include <map>
#include "btparser.h"
#include "dllexer.h"
-#include "macro.h"
+
+class Macro{};
class DLParser : public BTParser
{
~DLParser();
AST* parse(void);
bool isMacro(Token& token);
- AST* parseMacroParam(Param* param);
bool speculate_GroupExpr(void);
/**********************************************************************
// Macro Rules
AST* MacroDefinition(void);
- AST* MacroExpansion(void);
AST* MacroPatternList(void);
AST* MacroPattern(void);
+++ /dev/null
-#include "macro.h"
-#include "macroapplication.h"
-#include "cork.h"
-
-using namespace std;
-
-Macro::Macro(AST* macro_def)
-{
- list<AST*>::iterator it = macro_def->children()->begin();
-
- // Set Name
- macro_name = (*it++)->text();
-
- // Set Body
- setUpParamList( *it++ );
-
- // Set Params
- macro_body = (*it++)->clone();
-
-}
-
-Macro::~Macro()
-{
-
- std::list<Param*>::iterator iter;
- for (iter = macro_params.begin(); iter != macro_params.end(); ++iter) {
- delete *iter;
- }
- delete macro_body;
-}
-
-const std::string& Macro::name(void)
-{
- return macro_name;
-}
-
-const std::list<Param*>& Macro::params(void)
-{
- return macro_params;
-}
-
-AST* Macro::apply(void)
-{
- MacroApplication application(macro_body->clone(), macro_params);
- application.visit();
- return application.getAST()->clone();
-}
-
-void Macro::setUpParamList( AST* param_tree )
-{
- list<AST*>::iterator it = param_tree->children()->begin();
- for(; it != param_tree->children()->end(); it++)
- {
- macro_params.push_back( _new Param( *it ) );
- }
-}
-
+++ /dev/null
-#ifndef MACRO_H
-#define MACRO_H
-
-#include <string>
-#include <list>
-#include "param.h"
-#include "ast.h"
-
-class Macro
-{
- private:
- std::string macro_name;
- std::list<Param*> macro_params;
- AST* macro_body;
-
- void setUpParamList( AST* param_tree );
- public:
- Macro(AST* macro_def);
- ~Macro();
-
- const std::string& name(void);
- const std::list<Param*>& params(void);
- AST* apply(void);
-};
-
-#endif
+++ /dev/null
-#include "param.h"
-
-Param::Param(AST* param_def)
-{
- int children = param_def->children()->size();
- if( children == 0 )
- {
- param_name = param_def->text();
- }
- else if( children == 2)
- {
- param_name = param_def->children()->front()->text();
- setType( param_def->children()->front()->text() );
- }
- else
- {
- // Throw
- }
-}
-
-Param::~Param()
-{
- if( param_value != NULL )
- {
- delete param_value;
- }
-}
-
-std::string Param::name(void)
-{
- return param_name;
-}
-
-ParamType_T Param::type(void)
-{
- return param_type;
-}
-
-AST* Param::value(void)
-{
- return param_value;
-}
-
-void Param::setValue(AST* val)
-{
- param_value = val;
-}
-
-void Param::setType( const std::string& type_string )
-{
- if ( type_string.compare("Block") == 0 )
- {
- param_type = BlockTyp;
- }
- else
- {
- param_type = ExpTyp;
- }
-}
-
+++ /dev/null
-#ifndef PARAM
-#define PARAM
-
-#include <string>
-#include "ast.h"
-
-typedef enum {
- ExpTyp,
- BlockTyp
-} ParamType_T;
-
-class Param
-{
- private:
- std::string param_name;
- ParamType_T param_type;
- AST* param_value;
-
- void setType( const std::string& type_string );
-
- public:
- Param(AST* param_def);
- ~Param();
-
- std::string name(void);
- ParamType_T type(void);
- AST* value(void);
- void setValue(AST* val);
-};
-
-#endif
+++ /dev/null
-#include "macroapplication.h"
-#include "dllexer.h"
-
-using namespace std;
-
-Param* MacroApplication::getParamByName(std::string name)
-{
- Param* ret = NULL;
- std::list<Param*>::iterator it = macro_params.begin();
- for(; it != macro_params.end(); it++)
- {
- if( (*it)->name().compare( name ) == 0)
- {
- ret = *it;
- break;
- }
- }
- return ret;
-}
-
-AST* MacroApplication::getAST(void)
-{
- return mod_ast;
-}
-
-void MacroApplication::beforeVisit(AST* cur, int depth)
-{
-}
-
-void MacroApplication::afterVisit(AST* cur, int depth)
-{
-}
-
-void MacroApplication::beforeChildren(AST* cur, int depth)
-{
- if(cur->type() == ID)
- {
- Param* param = getParamByName( cur->text() );
- if( param != NULL )
- {
- (*cur) = *(param->value());
- }
- }
-}
-
-void MacroApplication::afterChildren(AST* cur, int depth)
-{
-}
-
-void MacroApplication::beforeChild(AST* cur, int depth)
-{
-}
-
-void MacroApplication::afterChild(AST* cur, int depth)
-{
-}
-
+++ /dev/null
-#ifndef MacroApplication_H
-#define MacroApplication_H
-
-#include "ivisitor.h"
-#include <iostream>
-#include <sstream>
-#include "param.h"
-
-class MacroApplication : public IVisitor {
- protected:
- AST* mod_ast;
- std::ostringstream stream;
- std::list<Param*> macro_params;
- public:
- MacroApplication(AST* root,std::list<Param*>& params) : IVisitor(root), mod_ast(root), macro_params(params) {};
- Param* getParamByName(std::string name);
- AST* getAST(void);
- private:
- void beforeVisit(AST* cur, int depth);
- void afterVisit(AST* cur, int depth);
- void beforeChildren(AST* cur, int depth);
- void afterChildren(AST* cur, int depth);
- void beforeChild(AST* cur, int depth);
- void afterChild(AST* cur, int depth);
-};
-
-#endif