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:
zik.saleeba 2010-07-03 16:10:15 +00:00
parent b4b5594a5d
commit 7700f11893
6 changed files with 55 additions and 6 deletions

32
picoc.c
View file

@ -1,6 +1,7 @@
#include "picoc.h"
#define CALL_MAIN "main();"
#define CALL_MAIN_NO_ARGS "main();"
#define CALL_MAIN_WITH_ARGS "main(__argc,__argv);"
/* initialise everything */
@ -34,6 +35,27 @@ void Cleanup()
/* platform-dependent code for running programs is in this file */
#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 ParamCount = 1;
@ -41,9 +63,9 @@ int main(int argc, char **argv)
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 -m <csource1.c>... - run a program without calling main\n");
" picoc -m <csource1.c>... - run a program without calling main()\n");
exit(1);
}
@ -65,11 +87,11 @@ int main(int argc, char **argv)
return 1;
}
for (; ParamCount < argc; ParamCount++)
for (; ParamCount < argc && strcmp(argv[ParamCount], "-") != 0; ParamCount++)
PlatformScanFile(argv[ParamCount]);
if (!DontRunMain)
Parse("startup", CALL_MAIN, strlen(CALL_MAIN), TRUE);
CallMain(argc - ParamCount, &argv[ParamCount]);
}
Cleanup();

View file

@ -286,6 +286,7 @@ extern struct ValueType TypeType;
extern struct ValueType FunctionType;
extern struct ValueType MacroType;
extern struct ValueType *CharPtrType;
extern struct ValueType *CharPtrPtrType;
extern struct ValueType *CharArrayType;
extern struct ValueType *VoidPtrType;
extern char *StrEmpty;

12
tests/31_args.c Normal file
View 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
View file

@ -0,0 +1,6 @@
hello world 5
arg 0: -
arg 1: arg1
arg 2: arg2
arg 3: arg3
arg 4: arg4

View file

@ -28,6 +28,7 @@ TESTS= 00_assignment.test \
28_strings.test \
29_array_address.test \
30_hanoi.test \
31_args.test \
34_array_assignment.test \
35_sizeof.test \
36_array_initialisers.test \
@ -38,7 +39,12 @@ TESTS= 00_assignment.test \
%.test: %.expect %.c
@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" ]; \
then \
echo "error in test $*"; \

2
type.c
View file

@ -18,6 +18,7 @@ struct ValueType FunctionType;
struct ValueType MacroType;
struct ValueType EnumType;
struct ValueType *CharPtrType;
struct ValueType *CharPtrPtrType;
struct ValueType *CharArrayType;
struct ValueType *VoidPtrType;
@ -139,6 +140,7 @@ void TypeInit()
#endif
CharArrayType = TypeAdd(NULL, &CharType, TypeArray, 0, StrEmpty, sizeof(char));
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 *));
}