Skip to content

Commit b148385

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

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

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: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ static void dostate(void);
142142
static void addwhile(int *ptr);
143143
static void delwhile(void);
144144
static int *readwhile(void);
145+
static void remember_builtin_string(const char *name,const char *value);
146+
static void register_builtin_string(void);
147+
static void delete_builtin_string(void);
145148

146149
typedef void (SC_FASTCALL *OPCODE_PROC)(char *name);
147150
typedef struct {
@@ -796,6 +799,7 @@ int pc_compile(int argc, char *argv[])
796799
#endif
797800
delete_autolisttable();
798801
delete_heaplisttable();
802+
delete_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)=='\"') { /* check if value starts with quote */
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+
remember_builtin_string(str,strval); /* store as string constant */
1266+
} else {
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,8 @@ 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();
15951610
}
15961611

15971612
static int getclassspec(int initialtok,int *fpublic,int *fstatic,int *fstock,int *fconst)
@@ -1664,6 +1679,59 @@ static int getclassspec(int initialtok,int *fpublic,int *fstatic,int *fstock,int
16641679
return err==0;
16651680
}
16661681

1682+
static void remember_builtin_string(const char *name,const char *value)
1683+
{
1684+
builtinstring *tbl=&builtin_strings;
1685+
int newsize;
1686+
void *p;
1687+
1688+
assert(tbl!=NULL);
1689+
1690+
/* Expand the array if necessary */
1691+
if (tbl->count >= tbl->size) {
1692+
newsize=(tbl->size==0) ? 2 : tbl->size * 2;
1693+
p=realloc(tbl->entries,newsize * sizeof(*tbl->entries));
1694+
1695+
if (p==NULL) {
1696+
error(103); /* insufficient memory */
1697+
return;
1698+
}
1699+
1700+
tbl->entries = p;
1701+
tbl->size = newsize;
1702+
}
1703+
1704+
/* Copy the name and value into the next available slot */
1705+
strlcpy(tbl->entries[tbl->count].name,name,sizeof(tbl->entries[tbl->count].name));
1706+
strlcpy(tbl->entries[tbl->count].value,value,sizeof(tbl->entries[tbl->count].value));
1707+
tbl->count++;
1708+
}
1709+
1710+
static void register_builtin_string(void)
1711+
{
1712+
builtinstring *tbl=&builtin_strings;
1713+
1714+
assert(tbl!=NULL);
1715+
1716+
/* Add each remembered string constant to the global symbol table */
1717+
for (int i=0; i<tbl->count; ++i) {
1718+
add_builtin_string_constant(tbl->entries[i].name,tbl->entries[i].value,sGLOBAL);
1719+
}
1720+
}
1721+
1722+
static void delete_builtin_string(void)
1723+
{
1724+
builtinstring *tbl=&builtin_strings;
1725+
1726+
assert(tbl!=NULL);
1727+
1728+
/* Release the entries array and reset counters */
1729+
free(tbl->entries);
1730+
tbl->entries=NULL;
1731+
tbl->count=0;
1732+
tbl->size=0;
1733+
}
1734+
16671735
/* parse - process all input text
16681736
*
16691737
* At this level, only static declarations and function definitions are legal.

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)