SRL Archive - C/C++ Help and Tutorials

interpreter


sherlockmeister

2008-05-25 21:54:10

hey, this is one of my first c++ projects that is sort of an interpreter for very simple c like syntax. all it has is the basic arithmetic, bit, logical operators, and two types (float and int). plus there are some "native" functions that i put in. there really isn't much control flow except for jump function that takes two args like this jump(int,label) and jumps to the label if the int is > 0. the interpreter code itself is kind of messy / inefficient / disorganized so there are probably some bugs. well, have fun and post any comments, questions, criticisms that you might have and tell me what you think.

(oh and run this from command line with name of file to compile)

here are simple some working scripts i made:

input a number and it find its greatest factor less than it
int number=abs(input()),root=sqrt(number),test=2,mod=0;
jump (number&1,CHECK);
jump(true,END);
CHECK:
test=1;
LOOP:
test+=2;
jump((test<root)&&(mod=number%test),LOOP);
END:
number/=mod?number:test;
println(number);

input 2 numbers and outputs their gcf and lcm
int a=input(),b=input(),c=a%b;
int a2=a,b2=b;
LOOP2:
jump(!c,BREAK);
a=b;
b=c;
c=a%b;
jump(true,LOOP2);
BREAK:
println(b);
println(a2*b2/b);

simple quadratic formula , input a,b,c out puts answers
float aa=inputf(),bb=inputf(),cc=inputf();
float d=sqrt((bb*bb)-(4*aa*cc));
println((-bb+d)/2/aa);
println((-bb-d)/2/aa);

Freddy1990

2008-05-30 18:07:19

Looks pretty nice, you should make it possible to easily enter functionpointers which would be stored to call the functions afterwards from the script