URL
https://opencores.org/ocsvn/zipcpu/zipcpu/trunk
Subversion Repositories zipcpu
[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zpp.l] - Rev 26
Go to most recent revision | Compare with Previous | Blame | View Log
/*******************************************************************************
**
** Filename: zpp.l
**
** Project: Zip CPU -- a small, lightweight, RISC CPU core
**
** Purpose: The preprocessor for the Zip Assembler.
**
** This routine strips comments, handles #define's, #ifdef's, and
** #include statements in a C fashion.
**
** #define macro's are also defined in the language and therefore
** supposed to be supported, but the support isn't there yet.
**
** Creator: Dan Gisselquist, Ph.D.
** Gisselquist Tecnology, LLC
**
********************************************************************************
**
** Copyright (C) 2015, Gisselquist Technology, LLC
**
** This program is free software (firmware): 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 3 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 MERCHANTIBILITY 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. (It's in the $(ROOT)/doc directory, run make with no
** target there if the PDF file isn't present.) If not, see
** <http://www.gnu.org/licenses/> for a copy.
**
** License: GPL, v3, as defined and found on www.gnu.org,
** http://www.gnu.org/licenses/gpl.html
**
**
*******************************************************************************/
%{
// #include <FlexLexer.h>
#include <string>
#include <ctype.h>
#include <stdio.h>
#include <list>
using namespace std;
int yylex();
void mark_line(void);
int end_of_file(void);
void pushf(const char *fname);
// #include "zprepr.tab.h"
int ndef = 0, structno = 0;
char *structid = NULL;
void stb_define(const char *str);
void stb_args(const char *str);
void stb_macro(const char *value);
void stb_addmacro(const char *value);
bool stb_isdefined(const char *str);
const char *stb_getdefn(const char *str);
%}
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID
%option noyywrap
%option stack
ID [_:A-Za-z][_:A-Za-z0-9]*
IDDOT {ID}("."{ID})*
%%
<COMMENT>"*/" { yy_pop_state(); }
<COMMENT>[^*]+ { /* Ignore comments */ }
<COMMENT>"*"+[^/] { /* Ignore comments */ }
<INITIAL,INDEF>^"#include"[ \t]+\"[^\"]+\" {
char *ptr = &yytext[9], *start, *end, *str;
while(isspace(*ptr))
ptr++;
start = ++ptr;
ptr++;
while((*ptr)&&(*ptr != '\"'))
ptr++;
*ptr = '\0';
pushf(start);
// push_file_state(yylineno); // and filename ...
mark_line();
}
<INITIAL,INDEF>^"#define"[ \t]+ { yy_push_state(DEF); }
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
<DEF>{IDDOT}/[^(] {
stb_define(yytext);
BEGIN DFV;
}
<DEF>{IDDOT}/[(] { stb_define(yytext); BEGIN DFA; }
<DFA>"("[^)]+")" {
/* Process macro arguments */
stb_args(yytext);
BEGIN DFV;
}
<DFV>[ \t]+ { /* Ignore initial spaces */ }
<DFV>[^ \t\n]* {/* Parse to end of line, get value for our define */
stb_macro(yytext);
}
<DFV>[ \t]*((;|"//").*)?$ {/* Parse to end of line, get value for our define */
yy_pop_state();
}
<DFV>[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
fprintf(yyout, "\n"); mark_line(); yylineno++;
stb_macro("\n");
}
<INITIAL,INDEF>^[ \t]+.[dD][aA][tT][aA]? { fprintf(yyout, "\tWORD"); }
<INITIAL,INDEF>^"#defcont"[ \t]+ { yy_push_state(DFV); }
<INITIAL,INDEF>^"#ifdef"[ \t]* { ndef = 0; yy_push_state(IFDEFV); }
<INITIAL,INDEF>^"#ifndef"[ \t]* { ndef = 1; yy_push_state(IFDEFV); }
<IFDEFV>{IDDOT} {
bool known = stb_isdefined(yytext);
if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
BEGIN INDEF_EOL;
} else {
BEGIN INNOTDEF_EOL;
}
}
/* Not yet: <INITIAL,INDEF>^"#if"[ \t]* { yy_push_state(IFDEFE); }
/* Not yet: <INITIAL,INDEF>^"#if"[ \t]* { yy_push_state(IFDEFE); }
/* Not yet: <IFDEFE><expr> { yy_push_state(IFDEFE); } */
/* <INNOTDEF>^"#elsif"[ \t]* { yy_pop_state(); yy_push_state(IFDEFE); } */
<INDEF_EOL>[ \t]*$ { BEGIN INDEF; }
<INDEF_EOL>(;|"//").*$ { BEGIN INDEF; }
<INNOTDEF_EOL>[ \t]*$ { BEGIN INNOTDEF; }
<INNOTDEF_EOL>(;|"//").*$ { BEGIN INNOTDEF; }
<INDEF_EOL>[^ \t\n].*$ { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); mark_line(); }
<INNOTDEF_EOL>[^ \t\n].*$ { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); mark_line(); }
<INDEF,NVRDEF>^"#else"[ \t]*((;|"//").*)?$ { BEGIN NODEF; }
<INNOTDEF>^"#else"[ \t]*((;|"//").*)?$ { BEGIN INDEF; }
<INNOTDEF>(.*) { }
<INDEF>^"#elsif"[ \t]* { BEGIN NVRDEF; }
<NVRDEF,NODEF,INDEF,INNOTDEF>^"#endif"[ \t]*((;|"//").*)?$ { yy_pop_state(); }
<NVRDEF,NODEF,INDEF,INNOTDEF>^"#endif"[ \t]*"/*" { BEGIN COMMENT; }
<*>^"#endif"[ \t]* { fprintf(stderr, "ERR: Unknown endif!!\n");}
<INITIAL,INDEF>^"#struct"[ \t]* {
yy_push_state(GETSTRUCTID); structno = 0; }
<*>^"#"{ID}[ \t]* { fprintf(stderr, "ERR: Unrecognized preprocessor instruction, %s\n", yytext);}
<GETSTRUCTID>{ID}/[ \t\n;/] { BEGIN INSTRUCT;
structid = strdup(yytext);
}
<INSTRUCT>{ID}("."{ID})* {
fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
<INSTRUCT>^"#endstruct".*$ { yy_pop_state(); }
/* Not yet: ^"#struct"[ \t]* {} */
/* Not yet: ^"#endstruct"[ \t]* {} */
/* Not yet: ^"#seg"[ \t]* {} */
<NODEF,NVRDEF>.* { /* Ignore everything in these states*/ }
<INITIAL,INDEF>{ID}/[^(] {
if (stb_isdefined(yytext))
fprintf(yyout, "%s", stb_getdefn(yytext));
else
fprintf(yyout, "%s", yytext);
}
<*>[ \t]*"//".*$ { /* Ignore (trailing) comment only lines */ }
<*>[ \t]*";".*$ { /* Ignore (trailing) comment only lines */ }
<*>"#warning".*$ { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
<*>"#error".*$ { fprintf(stderr, "ERROR: %s\n", &yytext[8]); exit(-1); }
<*>"/*" { yy_push_state(COMMENT); }
<*>[ \t]+ { ECHO; }
<*>\n { ECHO; yylineno++; mark_line(); }
/* <*>. { printf("Unmatched \'%c\'\n", yytext[0]); } */
<NVRDEF,NODEF,INDEF,INNOTDEF><<EOF>> { fprintf(stderr, "Unexpected EOF! Expecting #endif\n"); }
<IFDEFV><<EOF>> { fprintf(stderr, "Unexpected EOF! Expecting #endif\n"); }
<COMMENT><<EOF>> { fprintf(stderr, "Unexpected EOF! Expecting */\n"); }
<INSTRUCT><<EOF>> { fprintf(stderr, "Unexpected EOF! Expecting #endstruct\n"); }
<GETSTRUCTID><<EOF>> { fprintf(stderr, "Unexpected EOF! Expecting #struct ID, then #endstruct\n"); }
<DFA,DFV,DFV_EOL><<EOF>> { fprintf(stderr, "Unexpected EOF! Expecting end of line\n"); }
<INDEF_EOL,INNOTDEF_EOL><<EOF> { fprintf(stderr, "Unexpected EOF Expecting end of line, then #endif\n"); }
<INITIAL><<EOF>> { if (end_of_file()) yyterminate(); }
%%
class SYMTABLE_ENTRY {
private:
std::string &trim(std::string &s) {
std::string::iterator ptr = s.end();
while((ptr >= s.begin())&&(isspace(*ptr)))
*ptr-- = '\0';
return s;
}
public:
std::string m_name, m_value, m_args;
SYMTABLE_ENTRY(const char *str) : m_name(str) {
trim(m_name);
}
SYMTABLE_ENTRY &operator+=(const char *str) {
const char *start = str;
while(isspace(*start))
start++;
if (m_value.length()!=0)
m_value += " ";
std::string trimd(start);
trim(trimd);
m_value += trimd;
/*
printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
m_name.c_str(), m_value.c_str());
*/
return *this;
}
SYMTABLE_ENTRY &setargs(const char *str) {
m_args += str;
return *this;
}
const std::string &getdefn(void) {
return m_value;
}
};
class SYMBOL_TABLE {
private:
typedef SYMTABLE_ENTRY *TBLV;
typedef std::list<TBLV> TBLT;
TBLT m_tbl;
TBLT::iterator lookup(const char *str) {
TBLT::iterator i = m_tbl.begin();
for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
;
if ((i != m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())==0))
return i;
return m_tbl.end();
}
public:
SYMBOL_TABLE(void) {}
void define(const char *str) {
SYMTABLE_ENTRY *v = new SYMTABLE_ENTRY(str);
TBLT::iterator i = m_tbl.begin();
for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
;
m_tbl.insert(i, v);
// printf("SYMS::Defining SYMBOL: \'%s\'\n", str);
}
bool defined(const char *str) {
TBLT::iterator i = lookup(str);
if (i==m_tbl.end())
return false;
else
return true;
}
void undefine(const char *str) {
TBLT::iterator i = lookup(str);
if (i == m_tbl.end())
return;
TBLV v = (*i);
m_tbl.erase(i);
delete v;
}
void addmacro(const char *name, const char *str) {
TBLT::iterator i = lookup(name);
if (i == m_tbl.end()) {
fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
} *(*i) += str;
}
void addargs(const char *name, const char *str) {
TBLT::iterator i = lookup(name);
if (i == m_tbl.end()) {
fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
} (*i)->setargs(str);
}
const char *getdefn(const char *name) {
TBLT::iterator i = lookup(name);
if (i == m_tbl.end()) {
fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
return NULL;
} (*i)->getdefn().c_str();
}
};
SYMTABLE_ENTRY *last = NULL;
SYMBOL_TABLE syms;
std::string last_define;
void stb_define(const char *str) {
if (syms.defined(str)) {
fprintf(stderr, "WARNING! Symbol \'%s\' is already defined!\n", str);
syms.undefine(str);
}
syms.define(str);
last_define = str;
}
void stb_args(const char *args) {
syms.addargs(last_define.c_str(), args);
}
void stb_macro(const char *value) {
syms.addmacro(last_define.c_str(), value);
}
void stb_addmacro(const char *value) {
syms.addmacro(last_define.c_str(),value);
}
bool stb_isdefined(const char *str) {
return syms.defined(str);
}
const char *stb_getdefn(const char *str) {
return syms.getdefn(str);
}
class BUFSTACK {
public:
FILE *m_fp;
char *m_fname;
int m_lineno;
BUFSTACK *m_prev;
YY_BUFFER_STATE m_bs;
static BUFSTACK *curbs;
static const char *curfilename;
BUFSTACK(void) {
m_fp = stdin;
if (curbs)
curbs->m_lineno = yylineno;
m_prev = curbs;
// m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
m_fname = strdup("(stdin)");
// yy_switch_to_buffer(m_bs);
m_bs = NULL;
curbs = this;
m_lineno = 1;
curfilename = m_fname;
yyrestart(m_fp);
yylineno = 1;
}
BUFSTACK(const char *fname) {
m_fp = fopen(fname, "r");
if (!m_fp) {
fprintf(stderr, "Cannot open %s\n", fname);
perror("O/S Err:");
exit(-1);
}
if (curbs)
curbs->m_lineno = yylineno;
m_prev = curbs;
m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
m_fname = strdup(fname);
yy_switch_to_buffer(m_bs);
curbs = this;
m_lineno = 1;
curfilename = m_fname;
yyrestart(m_fp);
yylineno = 1;
}
~BUFSTACK(void) {
// fprintf(stderr, "DELETING(%s)\n", m_fname);
fclose(m_fp);
free(m_fname);
if (m_bs)
yy_delete_buffer(m_bs);
curbs = m_prev;
if (curbs) {
yy_switch_to_buffer(curbs->m_bs);
yylineno = curbs->m_lineno;
curfilename = curbs->m_fname;
}
}
void mark(void) {
FILE *fp = yyout;
if (!fp) fp = stdout;
fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
}
void markline(void) {
FILE *fp = yyout;
if (!fp) fp = stdout;
fprintf(fp, "#line %d\n", yylineno);
}
static void pop(void) {
// fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
if (curbs)
delete curbs;
}
};
BUFSTACK *BUFSTACK::curbs = NULL;
const char *BUFSTACK::curfilename = NULL;
int last_marked_line = -1;
const char *last_marked_file = NULL;
void mark_line(void) {
if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
if (BUFSTACK::curfilename == last_marked_file)
BUFSTACK::curbs->markline();
else BUFSTACK::curbs->mark();
last_marked_line = yylineno;
last_marked_file = BUFSTACK::curfilename;
}
int end_of_file(void) {
BUFSTACK::pop();
return (BUFSTACK::curbs == NULL);
}
void pushf(const char *fname) {
BUFSTACK *bs = new BUFSTACK(fname);
}
int main(int argc, char **argv) {
yylineno = 1;
if (argc < 2) { // Stdin only
BUFSTACK::curbs = new BUFSTACK();
yylex();
} else {
for(int argn=1; argn<argc; argn++) {
BUFSTACK *bs = new BUFSTACK(argv[argn]);
mark_line();
yylex();
// delete bs;
}
}
return 0;
}
Go to most recent revision | Compare with Previous | Blame | View Log