Skip to content

Commit b12c2e7

Browse files
author
gskeleton
committed
feat: string constants via sym=val (define constant)
pull: openmultiplayer#12
1 parent 559e6a1 commit b12c2e7

3 files changed

Lines changed: 90 additions & 2 deletions

File tree

source/compiler/sc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ typedef struct s_valuepair {
303303
long second;
304304
} valuepair;
305305

306+
typedef struct s_builtinstring {
307+
struct {
308+
char name[sNAMEMAX+1];
309+
char value[_MAX_PATH];
310+
} *entries;
311+
int count;
312+
int size;
313+
} builtinstring;
314+
306315
/* macros for code generation */
307316
#define opcodes(n) ((n)*sizeof(cell)) /* opcode size */
308317
#define opargs(n) ((n)*sizeof(cell)) /* size of typical argument */
@@ -884,6 +893,8 @@ SC_VDECL int pc_recursion; /* enable detailed recursion report? */
884893
SC_VDECL constvalue_root sc_automaton_tab; /* automaton table */
885894
SC_VDECL constvalue_root sc_state_tab; /* state table */
886895

896+
SC_VDECL builtinstring builtin_strings; /* string constants */
897+
887898
SC_VDECL FILE *inpf; /* file read from (source or include) */
888899
SC_VDECL FILE *inpf_org; /* main source file */
889900
SC_VDECL FILE *outf; /* file written to */

source/compiler/sc1.c

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ static void reduce_referrers(symbol *root);
118118
static long max_stacksize(symbol *root,int *recursion);
119119
static int testsymbols(symbol *root,int level,int testlabs,int testconst);
120120
static void destructsymbols(symbol *root,int level);
121+
static void store_builtin_string(const char *name,const char *value);
122+
static void register_builtin_string(void);
123+
static void unregister_builtin_string(void);
121124
static constvalue *find_constval_byval(constvalue_root *table,cell val);
122125
static symbol *fetchlab(char *name);
123126
static void statement(int *lastindent,int allow_decl);
@@ -796,6 +799,7 @@ int pc_compile(int argc, char *argv[])
796799
#endif
797800
delete_autolisttable();
798801
delete_heaplisttable();
802+
unregister_builtin_string();
799803
if (errnum!=0) {
800804
if (strlen(errfname)==0)
801805
pc_printf("\n%d Error%s.\n",errnum,(errnum>1) ? "s" : "");
@@ -1252,8 +1256,17 @@ static void parseoptions(int argc,char **argv,char *oname,char *ename,char *pnam
12521256
error(200,argv[arg],sNAMEMAX); /* symbol too long, truncated to sNAMEMAX chars */
12531257
} /* if */
12541258
strlcpy(str,argv[arg],i+1); /* str holds symbol name */
1255-
i=atoi(ptr+1);
1256-
add_builtin_constant(str,i,sGLOBAL,0);
1259+
if (*(ptr+1)=='\"') { /* string constant */
1260+
char strval[_MAX_PATH];
1261+
strlcpy(strval,ptr+2,sizeof(strval)); /* copy string value (skip =") */
1262+
int len=strlen(strval);
1263+
if (len>0 && strval[len-1]=='\"') /* remove trailing quote */
1264+
strval[len-1]='\0';
1265+
store_builtin_string(str,strval); /* store as string constant */
1266+
} else { /* integar constant */
1267+
i=atoi(ptr+1); /* convert numeric value */
1268+
add_builtin_constant(str,i,sGLOBAL,0); /* store as integer constant */
1269+
}
12571270
} else if (oname) {
12581271
strlcpy(str,argv[arg],sizeof(str)-2); /* -2 because default extension is ".p" */
12591272
set_extension(str,".p",FALSE);
@@ -1592,6 +1605,68 @@ static void setstringconstants()
15921605
add_builtin_string_constant("__time",timebuf,sGLOBAL);
15931606
strftime(datebuf,sizeof(datebuf),"%d %b %Y",localtime(&now));
15941607
add_builtin_string_constant("__date",datebuf,sGLOBAL);
1608+
1609+
register_builtin_string();
1610+
}
1611+
1612+
static void store_builtin_string(const char *name,const char *value)
1613+
{
1614+
builtinstring *tbl=&builtin_strings;
1615+
int newsize;
1616+
void *p;
1617+
1618+
assert(tbl!=NULL);
1619+
1620+
/* Expand the array if necessary */
1621+
if (tbl->count >= tbl->size) {
1622+
newsize=(tbl->size==0) ? 2 : tbl->size * 2;
1623+
p=realloc(tbl->entries,newsize * sizeof(*tbl->entries));
1624+
1625+
if (p==NULL) {
1626+
error(103); /* insufficient memory */
1627+
return;
1628+
}
1629+
1630+
tbl->entries=p;
1631+
tbl->size=newsize;
1632+
}
1633+
1634+
/*
1635+
* Copy the name and value into the next available slot
1636+
*/
1637+
strlcpy(tbl->entries[tbl->count].name,name,sizeof(tbl->entries[tbl->count].name));
1638+
strlcpy(tbl->entries[tbl->count].value,value,sizeof(tbl->entries[tbl->count].value));
1639+
tbl->count++;
1640+
}
1641+
1642+
static void register_builtin_string(void)
1643+
{
1644+
builtinstring *tbl=&builtin_strings;
1645+
1646+
assert(tbl!=NULL);
1647+
1648+
/*
1649+
* Add each remembered string constant to the global symbol table
1650+
*/
1651+
for (int i = 0; i < tbl->count; ++i) {
1652+
char *name=tbl->entries[i].name;
1653+
const char *value=tbl->entries[i].value;
1654+
int vclass=sGLOBAL;
1655+
add_builtin_string_constant(name, value, vclass);
1656+
}
1657+
}
1658+
1659+
static void unregister_builtin_string(void)
1660+
{
1661+
builtinstring *tbl=&builtin_strings;
1662+
1663+
assert(tbl!=NULL);
1664+
1665+
/* Release the entries array and reset counters */
1666+
free(tbl->entries);
1667+
tbl->entries=NULL;
1668+
tbl->count=0;
1669+
tbl->size=0;
15951670
}
15961671

15971672
static int getclassspec(int initialtok,int *fpublic,int *fstatic,int *fstock,int *fconst)

source/compiler/scvars.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ SC_VDEFINE int pc_recursion=FALSE; /* enable detailed recursion report?
9898
SC_VDEFINE constvalue_root sc_automaton_tab = { NULL, NULL}; /* automaton table */
9999
SC_VDEFINE constvalue_root sc_state_tab = { NULL, NULL}; /* state table */
100100

101+
SC_VDEFINE builtinstring builtin_strings={ NULL, 0, 0 }; /* string constants */
102+
101103
SC_VDEFINE FILE *inpf = NULL; /* file read from (source or include) */
102104
SC_VDEFINE FILE *inpf_org= NULL; /* main source file */
103105
SC_VDEFINE FILE *outf = NULL; /* (intermediate) text file written to */

0 commit comments

Comments
 (0)