Can now call with command line arguments.
git-svn-id: http://picoc.googlecode.com/svn/trunk@453 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
parent
b4b5594a5d
commit
7700f11893
32
picoc.c
32
picoc.c
|
@ -1,6 +1,7 @@
|
||||||
#include "picoc.h"
|
#include "picoc.h"
|
||||||
|
|
||||||
#define CALL_MAIN "main();"
|
#define CALL_MAIN_NO_ARGS "main();"
|
||||||
|
#define CALL_MAIN_WITH_ARGS "main(__argc,__argv);"
|
||||||
|
|
||||||
|
|
||||||
/* initialise everything */
|
/* initialise everything */
|
||||||
|
@ -34,6 +35,27 @@ void Cleanup()
|
||||||
|
|
||||||
/* platform-dependent code for running programs is in this file */
|
/* platform-dependent code for running programs is in this file */
|
||||||
#ifdef UNIX_HOST
|
#ifdef UNIX_HOST
|
||||||
|
void CallMain(int argc, char **argv)
|
||||||
|
{
|
||||||
|
/* check if the program wants arguments */
|
||||||
|
struct Value *FuncValue;
|
||||||
|
|
||||||
|
VariableGet(NULL, TableStrRegister("main"), &FuncValue);
|
||||||
|
if (FuncValue->Typ->Base != TypeFunction)
|
||||||
|
ProgramFail(NULL, "main is not a function - can't call it");
|
||||||
|
|
||||||
|
if (FuncValue->Val->FuncDef.NumParams == 0)
|
||||||
|
Parse("", CALL_MAIN_NO_ARGS, strlen(CALL_MAIN_NO_ARGS), TRUE);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* define the arguments */
|
||||||
|
VariableDefinePlatformVar(NULL, "__argc", &IntType, (union AnyValue *)&argc, FALSE);
|
||||||
|
VariableDefinePlatformVar(NULL, "__argv", CharPtrPtrType, (union AnyValue *)&argv, FALSE);
|
||||||
|
|
||||||
|
Parse("", CALL_MAIN_WITH_ARGS, strlen(CALL_MAIN_WITH_ARGS), TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int ParamCount = 1;
|
int ParamCount = 1;
|
||||||
|
@ -41,9 +63,9 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
printf("Format: picoc <csource1.c>... - run a program (calls main to start it)\n"
|
printf("Format: picoc <csource1.c>... - run a program (calls main() to start it)\n"
|
||||||
" picoc -i - interactive mode\n"
|
" picoc -i - interactive mode\n"
|
||||||
" picoc -m <csource1.c>... - run a program without calling main\n");
|
" picoc -m <csource1.c>... - run a program without calling main()\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,11 +87,11 @@ int main(int argc, char **argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; ParamCount < argc; ParamCount++)
|
for (; ParamCount < argc && strcmp(argv[ParamCount], "-") != 0; ParamCount++)
|
||||||
PlatformScanFile(argv[ParamCount]);
|
PlatformScanFile(argv[ParamCount]);
|
||||||
|
|
||||||
if (!DontRunMain)
|
if (!DontRunMain)
|
||||||
Parse("startup", CALL_MAIN, strlen(CALL_MAIN), TRUE);
|
CallMain(argc - ParamCount, &argv[ParamCount]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Cleanup();
|
Cleanup();
|
||||||
|
|
1
picoc.h
1
picoc.h
|
@ -286,6 +286,7 @@ extern struct ValueType TypeType;
|
||||||
extern struct ValueType FunctionType;
|
extern struct ValueType FunctionType;
|
||||||
extern struct ValueType MacroType;
|
extern struct ValueType MacroType;
|
||||||
extern struct ValueType *CharPtrType;
|
extern struct ValueType *CharPtrType;
|
||||||
|
extern struct ValueType *CharPtrPtrType;
|
||||||
extern struct ValueType *CharArrayType;
|
extern struct ValueType *CharArrayType;
|
||||||
extern struct ValueType *VoidPtrType;
|
extern struct ValueType *VoidPtrType;
|
||||||
extern char *StrEmpty;
|
extern char *StrEmpty;
|
||||||
|
|
12
tests/31_args.c
Normal file
12
tests/31_args.c
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int Count;
|
||||||
|
|
||||||
|
printf("hello world %d\n", argc);
|
||||||
|
for (Count = 0; Count < argc; Count++)
|
||||||
|
printf("arg %d: %s\n", Count, argv[Count]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
6
tests/31_args.expect
Normal file
6
tests/31_args.expect
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
hello world 5
|
||||||
|
arg 0: -
|
||||||
|
arg 1: arg1
|
||||||
|
arg 2: arg2
|
||||||
|
arg 3: arg3
|
||||||
|
arg 4: arg4
|
|
@ -28,6 +28,7 @@ TESTS= 00_assignment.test \
|
||||||
28_strings.test \
|
28_strings.test \
|
||||||
29_array_address.test \
|
29_array_address.test \
|
||||||
30_hanoi.test \
|
30_hanoi.test \
|
||||||
|
31_args.test \
|
||||||
34_array_assignment.test \
|
34_array_assignment.test \
|
||||||
35_sizeof.test \
|
35_sizeof.test \
|
||||||
36_array_initialisers.test \
|
36_array_initialisers.test \
|
||||||
|
@ -38,7 +39,12 @@ TESTS= 00_assignment.test \
|
||||||
|
|
||||||
%.test: %.expect %.c
|
%.test: %.expect %.c
|
||||||
@echo Test: $*...
|
@echo Test: $*...
|
||||||
-@../picoc -m $*.c 2>&1 >$*.output
|
@if [ "x`echo $* | grep args`" != "x" ]; \
|
||||||
|
then \
|
||||||
|
../picoc $*.c - arg1 arg2 arg3 arg4 2>&1 >$*.output; \
|
||||||
|
else \
|
||||||
|
../picoc -m $*.c 2>&1 >$*.output; \
|
||||||
|
fi
|
||||||
@if [ "x`diff -qbu $*.expect $*.output`" != "x" ]; \
|
@if [ "x`diff -qbu $*.expect $*.output`" != "x" ]; \
|
||||||
then \
|
then \
|
||||||
echo "error in test $*"; \
|
echo "error in test $*"; \
|
||||||
|
|
2
type.c
2
type.c
|
@ -18,6 +18,7 @@ struct ValueType FunctionType;
|
||||||
struct ValueType MacroType;
|
struct ValueType MacroType;
|
||||||
struct ValueType EnumType;
|
struct ValueType EnumType;
|
||||||
struct ValueType *CharPtrType;
|
struct ValueType *CharPtrType;
|
||||||
|
struct ValueType *CharPtrPtrType;
|
||||||
struct ValueType *CharArrayType;
|
struct ValueType *CharArrayType;
|
||||||
struct ValueType *VoidPtrType;
|
struct ValueType *VoidPtrType;
|
||||||
|
|
||||||
|
@ -139,6 +140,7 @@ void TypeInit()
|
||||||
#endif
|
#endif
|
||||||
CharArrayType = TypeAdd(NULL, &CharType, TypeArray, 0, StrEmpty, sizeof(char));
|
CharArrayType = TypeAdd(NULL, &CharType, TypeArray, 0, StrEmpty, sizeof(char));
|
||||||
CharPtrType = TypeAdd(NULL, &CharType, TypePointer, 0, StrEmpty, sizeof(void *));
|
CharPtrType = TypeAdd(NULL, &CharType, TypePointer, 0, StrEmpty, sizeof(void *));
|
||||||
|
CharPtrPtrType = TypeAdd(NULL, CharPtrType, TypePointer, 0, StrEmpty, sizeof(void *));
|
||||||
VoidPtrType = TypeAdd(NULL, &VoidType, TypePointer, 0, StrEmpty, sizeof(void *));
|
VoidPtrType = TypeAdd(NULL, &VoidType, TypePointer, 0, StrEmpty, sizeof(void *));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue