Mercurial > sos > sos
changeset 2:b6182f00de82
The work I have so far
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Tue, 01 Mar 2011 02:39:27 -0500 |
parents | 29382f5864ca |
children | 0aa0ad9e1cc3 |
files | Makefile include/operations.h include/stack.h include/std.h src/init.c src/operations.c src/stack.c src/std.c |
diffstat | 8 files changed, 257 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Fri Feb 25 18:58:43 2011 -0500 +++ b/Makefile Tue Mar 01 02:39:27 2011 -0500 @@ -8,7 +8,7 @@ CFLAGS=-g -fno-strict-aliasing -fno-builtin -nostdinc -nostdlib -Wall -m64 -I include/ -O2 LDFLAGS=-m elf64_s390 -OBJS=src/init.o src/io.o +OBJS=src/init.o src/io.o src/std.o src/stack.o src/operations.o .PHONY: all build clean tags
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/operations.h Tue Mar 01 02:39:27 2011 -0500 @@ -0,0 +1,17 @@ +#ifndef __OPERATIONS_H +#define __OPERATIONS_H + +#include <std.h> +#include <stack.h> + +enum Operation {PRINT, ADD, SUB, MULT, DIV, DUP, MAXOP}; +const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "dup"}; + +void print(struct Stack *stack); +void add(struct Stack *stack); +void sub(struct Stack *stack); +void mult(struct Stack *stack); +void div(struct Stack *stack); +void dup(struct Stack *stack); + +#endif //__OPERATIONS_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/stack.h Tue Mar 01 02:39:27 2011 -0500 @@ -0,0 +1,16 @@ +#ifndef __STACK_H +#define __STACK_H + +typedef int eltType; + +struct Stack { + int isEmpty; + unsigned int top; + eltType values[100]; +}; + +eltType pop(struct Stack *stack); +void push(struct Stack *stack, eltType val); +void initStack(struct Stack *stack); + +#endif //__STACK_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/std.h Tue Mar 01 02:39:27 2011 -0500 @@ -0,0 +1,15 @@ +#ifndef __STD_H +#define __STD_H + +extern void itoa(int n, char *a); +extern int atoi(char *a); + +extern void sPrint(char *a); +void iPrint(int n); +extern void sGet(char *a, unsigned int n); + +extern int strcmp(char *a, char *b); + +int arrayLookup(char *text, const char array[][10], int last); + +#endif
--- a/src/init.c Fri Feb 25 18:58:43 2011 -0500 +++ b/src/init.c Tue Mar 01 02:39:27 2011 -0500 @@ -1,9 +1,65 @@ /* * This is where everything starts */ + +#include <std.h> +#include <operations.h> +#include <stack.h> + +void dumpBuffer(char *a, int b) +{ + int i; + for(i = 0; i < b; i++) { + int foo = (int) a[i]; + char c[10]; + itoa(foo, c); + sPrint(c); + sPrint("\n"); + } +} + void start(u64 __memsize) { - putline("Hello\n", 6); + /* + int foo; + char bar[20]; + char string[20] = "This is a test"; + dumpBuffer(string, 10); + sPrint("Please enter an operation: "); + foo = getline(string, 10); + itoa(foo, bar); + sPrint(bar); + sPrint("\n"); + dumpBuffer(string, 10); + sPrint("Please enter a second operation: "); + sGet(string, 4); + dumpBuffer(string, 10); + */ + + void (*operation[MAXOP])(struct Stack*) = {NULL}; + operation[0] = print; + operation[1] = add; + operation[2] = sub; + operation[3] = mult; + operation[4] = div; + operation[5] = dup; + + struct Stack theStack; + struct Stack *stack = &theStack; + initStack(stack); + while(1) { + int opVal; + char input[20]; + sPrint("Please enter an operation or value: "); + sGet(input, 20); + opVal = arrayLookup(input, operationNames, MAXOP); + if(opVal != MAXOP) { + (*operation[opVal]) (stack); //call the function array + } else { + opVal = atoi(input); + push(stack, opVal); + } + } for(;;) ;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/operations.c Tue Mar 01 02:39:27 2011 -0500 @@ -0,0 +1,47 @@ +//#include <operations.h> +#include <std.h> +#include <stack.h> + +void print(struct Stack *stack) +{ + eltType val = pop(stack); + char output[10]; + itoa(val, output); + sPrint(output); + sPrint("\n"); +} + +void add(struct Stack *stack) +{ + eltType first = pop(stack); + eltType sec = pop(stack); + push(stack, sec + first); //yeah, this should be obvious +} + +void sub(struct Stack *stack) +{ + eltType first = pop(stack); + eltType sec = pop(stack); + push(stack, sec - first); +} + +void mult(struct Stack *stack) +{ + eltType first = pop(stack); + eltType sec = pop(stack); + push(stack, sec*first); +} + +void div(struct Stack *stack) +{ + eltType first = pop(stack); + eltType sec = pop(stack); + push(stack, sec/first); +} + +void dup(struct Stack *stack) +{ + eltType val = pop(stack); + push(stack, val); + push(stack, val); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/stack.c Tue Mar 01 02:39:27 2011 -0500 @@ -0,0 +1,32 @@ +#include <stack.h> + +eltType pop(struct Stack *stack) +{ + if(stack->isEmpty) { + sPrint("ERROR: STACK IS EMPTY.\n"); + return -1; + } else if(stack->top == 0) { + stack->isEmpty = 1; + return stack->values[stack->top]; + } else if(stack->top < 0) { + return 0; //TODO figure out a better way to handle errors + } else { + return stack->values[stack->top--]; + } +} + +void push(struct Stack *stack, eltType val) +{ + if(stack->isEmpty) { + stack->isEmpty = 0; + stack->values[stack->top] = val; + } else { + stack->values[++stack->top] = val; + } +} + +void initStack(struct Stack *stack) +{ + stack->isEmpty = 1; + stack->top = 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/std.c Tue Mar 01 02:39:27 2011 -0500 @@ -0,0 +1,72 @@ +void itoa(int n, char *a) +{ + if(n < 0) { + *a++ = '-'; + n *= -1; + } + char *b = a; + if(!n) *b++ = '0'; + for(; n; b++) { + *b = n%10 + '0'; + n = n/10; + } + *b-- = '\0'; + for(; a < b; a++, b--) { //reverse + char temp = *b; + *b = *a; + *a = temp; + } +} + +int atoi(char *a) +{ + int neg = 0; + int n = 0; + if(*a == '-') { + neg = 1; + a++; + } + while(*a >='0' && *a <= '9') + n = n*10 + (*a++ - '0'); + if(neg) n *= -1; + return n; +} + +void sPrint(char *a) +{ + char *b = a; + while(*b && *++b); + putline(a, b - a); +} + +void iPrint(int n) +{ + char string[10]; + itoa(n, string); + sPrint(string); +} + +int strcmp(char *a, char *b) +{ + while(1) { + if(*a - *b) return *a - *b; + if(*a == '\0') return 0; + a++; + b++; + } + return -1; +} + +void sGet(char *a, unsigned int n) //TODO bug Jeff about getline +{ + int length = getline(a, n); + a[(length < n)?length:n - 1] = '\0'; +} + +int arrayLookup(char *text, const char array[][10], int last) +{ + int i; + for(i = 0; i < last; i++) + if(!strcmp(array[i], text)) return i; + return last; +}