Mercurial > sos > sos
changeset 14:94ed54424389
Merge again
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Wed, 09 Mar 2011 18:24:04 -0500 |
parents | 2356e9d59bc4 (diff) e2b1d6184703 (current diff) |
children | 525eab23e68a |
files | |
diffstat | 10 files changed, 467 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Wed Mar 09 18:21:44 2011 -0500 +++ b/Makefile Wed Mar 09 18:24:04 2011 -0500 @@ -10,7 +10,7 @@ BINS=sarpn -sarpn_OBJS=src/init.o arch/io.o +sarpn_OBJS=src/init.o src/io.o src/std.o src/stack.o src/operations.o src/math.o .PHONY: all build clean tags @@ -66,3 +66,14 @@ ipl/%.o: ipl/%.S $(AS) -m64 -o $@ $< + +depend: + makedepend -I include src/*.c + +# DO NOT DELETE + +src/init.o: include/std.h include/operations.h include/stack.h +src/operations.o: include/operations.h include/std.h include/stack.h +src/operations.o: include/math.h +src/stack.o: include/stack.h include/std.h +src/std.o: include/std.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/math.h Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,15 @@ +#ifndef __MATH_H +#define __MATH_H + +double math_iPow(double base, unsigned int exp); +double math_exp(double exp); +double math_log(double num); +double math_pow(double base, double exp); +double math_abs(double num); + +double math_add(double first, double sec); +double math_sub(double first, double sec); +double math_mult(double first, double sec); +double math_div(double first, double sec); + +#endif //__MATH_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/operations.h Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,25 @@ +#ifndef __OPERATIONS_H +#define __OPERATIONS_H + +#include <std.h> +#include <stack.h> + +enum Operation {PRINT, ADD, SUB, MULT, DIV, POW, DUP, DROP, LOG, EXP, MAXOP}; +extern const char operationNames[MAXOP][10]; +extern void (*operation[MAXOP])(struct Stack*); + +void op_math1(struct Stack *stack, eltType (*mathop)(eltType)); +void op_math2(struct Stack *stack, eltType (*mathop)(eltType, eltType)); + +void op_print(struct Stack *stack); +void op_add(struct Stack *stack); +void op_sub(struct Stack *stack); +void op_mult(struct Stack *stack); +void op_div(struct Stack *stack); +void op_pow(struct Stack *stack); +void op_dup(struct Stack *stack); +void op_drop(struct Stack *stack); +void op_log(struct Stack *stack); +void op_exp(struct Stack *stack); + +#endif //__OPERATIONS_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/stack.h Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,21 @@ +#ifndef __STACK_H +#define __STACK_H + +typedef double eltType; +typedef struct eltTypeCon eltCon; + +struct Stack { + int top; + eltType values[100]; +}; + +struct eltTypeCon { //contains an eltType and an error value + eltType val; + short error; +}; + +eltCon 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 Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,18 @@ +#ifndef __STD_H +#define __STD_H + +char* ftoa(double x, char *a, unsigned int prec); +char* itoa(int n, char *a); +int atoi(char *a); +double atof(char *a); + +void sPrint(char *a); +char* sGet(char *a, unsigned int n); + +int strcmp(const char *a, const char *b); + +int arrayLookup(char *text, const char array[][10], int last); + +char* append(char *dest, char *src); + +#endif
--- a/src/init.c Wed Mar 09 18:21:44 2011 -0500 +++ b/src/init.c Wed Mar 09 18:24:04 2011 -0500 @@ -1,9 +1,55 @@ /* * 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); + */ + + struct Stack theStack; + struct Stack *stack = &theStack; + initStack(stack); + while(1) { + int opVal; + char input[30]; + sPrint("Please enter an operation or value: "); + opVal = arrayLookup(sGet(input, 30), operationNames, MAXOP); + if(opVal != MAXOP) { + (*operation[opVal]) (stack); //call the function array + } else { + push(stack, atof(input)); + } + } for(;;) ;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/math.c Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,90 @@ +double math_iPow(double base, int exp) +{ + double ans = 1; + short isNeg = 0; + if(exp < 0) { + isNeg = 1; + exp *= -1; + } + while(exp > 0) { + if(exp%2) { + exp -= 1; + ans *= base; + } else { + exp /= 2; + base *= base; + } + } + return isNeg ? 1/ans : ans; +} + +double math_exp(double exp) +{ + unsigned int i; //a counter for the loop + double ans = 1; + double prev = -1; + double term = 1; + short isNeg = 0; + if(exp < 0) { + isNeg = 1; + exp *= -1; + } + for(i = 1; ans - prev; i++) { + prev = ans; + term *= exp/i; + ans += term; + } + return isNeg ? 1/ans : ans; +} + +double math_log(double num) +{ + unsigned int denom = 1; + double euler = 2.71828182845904; + double ans = 0; + double prev = -1; + double term = -1; + while(num <= -1 || num >= 1) { + num /= euler; + ans += 1; + } + for(; ans - prev; denom++) { + prev = ans; + term *= (-1*(num - 1)); + ans += term/denom; + } + return ans; +} + +double math_pow(double base, double exp) +{ + if(exp == (int) exp) + return math_iPow(base, (int) exp); + return math_exp(exp*math_log(base)); +} + +double math_abs(double num) +{ + if(num < 0) return num*-1; + return num; +} + +double math_add(double first, double sec) +{ + return first + sec; +} + +double math_sub(double first, double sec) +{ + return first - sec; +} + +double math_mult(double first, double sec) +{ + return first*sec; +} + +double math_div(double first, double sec) +{ + return first/sec; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/operations.c Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,91 @@ +#include <operations.h> +#include <std.h> +#include <stack.h> +#include <math.h> + +const char operationNames[MAXOP][10] = {".", "+", "-", "*", "/", "**", "dup", "drop", "log", "exp"}; +void (*operation[MAXOP])(struct Stack*) = {op_print, op_add, op_sub, op_mult, + op_div, op_pow, op_dup, op_drop, op_log, op_exp}; + +void op_math1(struct Stack *stack, eltType (*mathop)(eltType)) +{ + if(stack->top < 0) sPrint("ERROR\n"); + else { + eltCon con = pop(stack); + if(con.error) sPrint("ERROR\n"); + else push(stack, mathop(con.val)); + } +} + +void op_math2(struct Stack *stack, eltType (*mathop)(eltType, eltType)) +{ + if(stack->top < 1) sPrint("ERROR\n"); + else { + eltCon con2 = pop(stack); + eltCon con1 = pop(stack); + if(con1.error || con2.error) sPrint("ERROR\n"); + else push(stack, mathop(con1.val, con2.val)); + } +} + +void op_print(struct Stack *stack) +{ + eltCon con = pop(stack); + if(con.error) sPrint("ERROR\n"); + else { + eltType val = con.val; + char output[30]; + sPrint(append(ftoa(val, output, 10), "\n")); + } +} + +void op_add(struct Stack *stack) +{ + op_math2(stack, math_add); +} + +void op_sub(struct Stack *stack) +{ + op_math2(stack, math_sub); +} + +void op_mult(struct Stack *stack) +{ + op_math2(stack, math_mult); +} + +void op_div(struct Stack *stack) +{ + op_math2(stack, math_div); +} + +void op_pow(struct Stack *stack) +{ + op_math2(stack, math_pow); +} + +void op_dup(struct Stack *stack) +{ + eltCon con = pop(stack); + if(con.error) sPrint("ERROR\n"); + else { + eltType val = con.val; + push(stack, val); + push(stack, val); + } +} + +void op_drop(struct Stack *stack) +{ + pop(stack); +} + +void op_log(struct Stack *stack) +{ + op_math1(stack, math_log); +} + +void op_exp(struct Stack *stack) +{ + op_math1(stack, math_exp); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/stack.c Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,24 @@ +#include <stack.h> +#include <std.h> + +eltCon pop(struct Stack *stack) +{ + eltCon ret; + if(stack->top == -1) { + ret.error = 1; + } else { + ret.error = 0; + ret.val = stack->values[stack->top--]; + } + return ret; +} + +void push(struct Stack *stack, eltType val) +{ + if(stack->top < (100 - 1)) stack->values[++stack->top] = val; +} + +void initStack(struct Stack *stack) +{ + stack->top = -1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/std.c Wed Mar 09 18:24:04 2011 -0500 @@ -0,0 +1,124 @@ +#include <std.h> + +char* itoa(int n, char *a) +{ + char *ret = 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; + } + return ret; +} + +char* ftoa(double x, char *a, unsigned int prec) +{ + char *ret = a; + int n = (int) x; //integer part + double d = x - (double) n; //fractional part; + itoa(n, a); + if(prec) { //only do the decimal part if decimal parts were asked for + while(*a && *++a); //get to the null character from itoa + int i; //counter variable for the for loop + *a++ = '.'; //put the decimal in place + for(i = 0; i < prec; i++) { + d = d*10; //the integer part is the decimal digit + *a++ = ((int) d) + '0'; //add the integer part of d to the string + d -= (int) d; //chop off the integer part + } a--; //move back to the last digit + while(*a != '.') { + if(*a == '0') { + a--; + continue; + } else if(*a > '0' && *a <= '9') { + a++; + break; + } + } + *a = '\0'; + } + return ret; +} + +int atoi(char *a) +{ + short neg = 0; + int n = 0; + if(*a == '-') { + neg = 1; + a++; + } else if(*a == '+') a++; + while(*a >= '0' && *a <= '9') + n = n*10 + (*a++ - '0'); + if(neg) n *= -1; + return n; +} + +double atof(char *a) +{ + int n = atoi(a); + double x = 0; + double dec = .1; + while(*a != '.') { + if(!(*a >= '0' && *a <= '9')) return n; + a++; + } a++; //a will be immediately after the decimal point + while(*a >= '0' && *a <= '9') { //goes through the decimal part + x += (*a - '0')*dec; + dec *= .1; + a++; + } + return n + x; +} + +void sPrint(char *a) +{ + char *b = a; + while(*b && *++b); + putline(a, b - a); +} + +int strcmp(const char *a, const char *b) +{ + while(1) { + if(*a - *b) return *a - *b; + if(*a == '\0') return 0; + a++; + b++; + } + return -1; +} + +char* sGet(char *a, unsigned int n) //TODO bug Jeff about getline +{ + int length = getline(a, n); + a[(length < n)?length:n - 1] = '\0'; + return a; +} + +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; +} + +char* append(char *dest, char *src) +{ + char *ret = dest; + while(*dest&& *++dest); //get to null in first string + while((*dest++ = *src++)); + return ret; +}