Ejemplo n.º 1
0
main()
{
    symtabADT table;
    scannerADT scanner;
    string line, name, value;

    table = NewSymbolTable();
    scanner = NewScanner();
    SetScannerSpaceOption(scanner, IgnoreSpaces);
    while (TRUE) {
        printf("-> ");
        line = GetLine();
        if (StringEqual(line, "quit")) break;
        SetScannerString(scanner, line);
        name = ReadToken(scanner);
        if (MoreTokensExist(scanner)) {
            if (!StringEqual(ReadToken(scanner), "=")) {
                Error("Missing equal sign in assignment");
            }
            value = ReadToken(scanner);
            if (MoreTokensExist(scanner)) {
                Error("Statement contains additional data");
            }
            Enter(table, name, value);
        } else {
            value = Lookup(table, name);
            if (value == UNDEFINED) {
                printf("'%s' is undefined.\n", name);
            } else {
                printf("%s\n", value);
            }
        }
    }
    FreeSymbolTable(table);
}
Ejemplo n.º 2
0
STStack NewSTStack()
{
    STStack t = malloc(sizeof(struct STStack_));
    t->stacksize = 100;
    t->sts = malloc(sizeof(SymbolTable) * t->stacksize);
    t->stacktop = -1;
    memset(t->sts, 0, t->stacksize * sizeof(SymbolTable));
    ST_Push(t, NewSymbolTable(COMST));
    return t;
}
Ejemplo n.º 3
0
main()
{
    cmdScannerADT cs;

    printf("Test program for the symbol table package\n");
    cs = NewCommandScanner();
    SetCommandData(cs, NewSymbolTable());
    InitCommandTable(cs);
    CommandLoop(cs, "-> ");
    FreeSymbolTable(GetCommandData(cs));
    FreeCommandScanner(cs);
}
Ejemplo n.º 4
0
cmdScannerADT NewCommandScanner(void)
{
    cmdScannerADT cs;

    cs = New(cmdScannerADT);
    cs->cmdTable = NewSymbolTable();
    cs->scanner = NewScanner();
    SetScannerSpaceOption(cs->scanner, IgnoreSpaces);
    cs->data = NULL;
    cs->quit = FALSE;
    return (cs);
}
Ejemplo n.º 5
0
main()
{
    cmdScannerADT cs;
    scannerADT scanner;
    graphADT graph;

    printf("Graph test program\n");
    cs = NewCommandScanner();
    scanner = GetTokenScanner(cs);
    SetScannerStringOption(scanner, ScanQuotesAsStrings);
    graph = NewGraph();
    SetGraphData(graph, NewSymbolTable());
    SetCommandData(cs, graph);
    InitCommandTable(cs);
    CommandLoop(cs, "G> ");
    FreeCommandScanner(cs);
}
Ejemplo n.º 6
0
speciesADT *SetSpecies(int *numcreature)
{
	string str;
	int numspecies=0,k;
	speciesADT *speciesP=GetBlock( MaxCreatures * sizeof (speciesADT) );
	speciesADT species;
	FILE *infile;
	symtabADT symtab=NewSymbolTable();

	while (TRUE)
	{
		printf("Input a species name (Food,Hop,Flytrap,Rover,Loner):  ");
		str=GetLine();
		if (StringEqual (str,"") && *numcreature==0)
		{
			printf("No.You must at least enter one species!\n");
		}
		else if (StringEqual(str,"") && *numcreature!=0)
		{
			break;
		}
		else 
		{
			infile=fopen(Concat("Creatures\\", str),"r");
			if (infile==NULL)  {printf("No such data file.Try again.\n");}
			else 
			{
				species=ReadSpecies(str);
				if (Lookup(symtab,SpeciesName(species))==UNDEFINED)
				{
					printf("%s ",SpeciesName(species));
					numspecies++;
					Enter (symtab,SpeciesName(species),species);
				}
				fclose(infile);
				for (k=*numcreature ; k<=*numcreature+9 ; k++)
				{
					speciesP[k]=species;
				}
				*numcreature = *numcreature + 10 ;
				if (*numcreature==MaxCreatures || numspecies==MaxSpecies)  { break; }
			}
		}
	}
	return speciesP;
}
Ejemplo n.º 7
0
bool WantToContinue(creatureADT *creatureP,int num,double *time)
{
	int i;
	int *k;
	string str;
	symtabADT symtab=NewSymbolTable();

	
	for (i=0;i<=num-1;i++)
	{
		if ( Lookup ( symtab  ,SpeciesName ( GetSpecies (creatureP[i]) ) )==UNDEFINED )
		{
			k=GetBlock(sizeof (int) );
			*k=1;
			Enter( symtab , SpeciesName ( GetSpecies (creatureP[i]) ) , k);
		}
		else 
		{
			k=(int*)Lookup(symtab,SpeciesName ( GetSpecies (creatureP[i]) ));
			*k=*k+1;
		}
	}
	printf("\nInventory:\n");
	MapSymbolTable(PrintInventory,symtab,NULL);
	FreeSymbolTable(symtab);

	ChangeTimeInterval(time);
	
	while(TRUE)
	{
		printf("\npress s+Enter to stop and c+Enter to continue :");
		str=GetLine();
		if (StringEqual(str,"s"))  {return FALSE;}
		if (StringEqual(str,"c")) 
		{
			printf("\nTo pause,click mouse on the graph.\n");
			return TRUE;
		}
	}
}
Ejemplo n.º 8
0
static void ClearCmdFn(cmdScannerADT cs)
{
    CheckForExtraTokens(cs);
    FreeSymbolTable(GetCommandData(cs));
    SetCommandData(cs, NewSymbolTable());
}
Ejemplo n.º 9
0
void InitVariableTable(void)
{
    variableTable = NewSymbolTable();
}