##- # Dedicated to the public domain # # Date: August 2016 # Modified: 2016-10-10/04:42-0400 ##+ # # slang.icn, load a S-Lang interpreter, and evaluate some statements # # tectonics: gcc -o slang.so -shared -fpic slang.c -lslang link ximage procedure main() # embed the interpreter slang := loadfunc("./slang.so", "slang") # return a computed variable, sum of list code := "variable slsum = sum([0,1,2,3,4,5,6,7,8,9]);_ slsum;" result := slang(code) write("Unicon sum: ", result) # return value is from S-Lang printf (bytes written) code := "printf(\"S-Lang: %f\\n\", slsum);" write("Unicon printf length: ", slang(code)) # S-Lang IO mix in code := "printf(\"S-Lang: %s = %f and %s = %f\\n\",_ \"hypot([3,4])\", hypot([3,4]),_ \"sumsq([3,4])\", sumsq([3,4]));" write("Unicon printf length: ", slang(code)) # 3D vector length code := "variable A = [3,4,5]; hypot(A);" write("Unicon hypot([3,4,5]): ", slang(code)) # try some strings, last one created will stay allocated code := "\"abc\";" write("Unicon abc: ", slang(code)) code := "\"def\";" write("Unicon def: ", slang(code)) # Pass an array, returned as a list of Real code := "[1, 2.2, 3, [4, 5, [6, 7], 8], 9.9];" write("Unicon from ", code) L := slang(code) writes("Unicon (array flattened) ") every i := !L do writes(i, " ") write() # Cummulative summation code := "cumsum([1.1, 2.2, 3.3, 4.4]);" L := slang(code) writes("Unicon from ", code, ": ") every i := !L do writes(i, " ") write() # try a small S-Lang program code := "variable t, i; t = 0; for (i = 0; i < 10; i++) t += i; t;" write("Unicon from ", code, ": ", slang(code)) # Exercise S-Lang load file write() write("Unicon run code from file slang.sl") slangfile := loadfunc("./slang.so", "slangFile") file := "slang.sl" # show the file cf := open(file, "r") | write("No ", file, " for test") write("####") while write(read(cf)) close(cf) write("####") # run the file L := slangfile(file) writes("Unicon from ", file, ": ") every i := !L do writes(i, " ") write() # convert an error to failure write() write("Unicon convert S-Lang error to failure") &error := 1 code := "[1, 2, \"abc\"];" write("Unicon trying: ", code) slang(code) write("Unicon S-Lang &errornumber: ", &errornumber) # and an abend write() write("Unicon abend on S-Lang divide by zero") code := "1/0" slang(code) end