├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CMakeLists.txt ├── LICENSE ├── README.md ├── bin └── output.dir ├── build ├── hgen.sgs ├── prep.sgs ├── vc10 │ ├── SGScript.sln │ ├── SGScript │ │ ├── SGScript.vcxproj │ │ └── SGScript.vcxproj.filters │ ├── sgsapitest │ │ ├── sgsapitest.vcxproj │ │ └── sgsapitest.vcxproj.filters │ ├── sgsc │ │ ├── sgsc.vcxproj │ │ └── sgsc.vcxproj.filters │ ├── sgsjson │ │ ├── sgsjson.vcxproj │ │ └── sgsjson.vcxproj.filters │ ├── sgsmeta │ │ ├── sgsmeta.vcxproj │ │ └── sgsmeta.vcxproj.filters │ ├── sgspproc │ │ ├── sgspproc.vcxproj │ │ └── sgspproc.vcxproj.filters │ ├── sgssockets │ │ ├── sgssockets.vcxproj │ │ └── sgssockets.vcxproj.filters │ ├── sgstest │ │ ├── sgstest.vcxproj │ │ └── sgstest.vcxproj.filters │ ├── sgsvm │ │ ├── sgsvm.vcxproj │ │ └── sgsvm.vcxproj.filters │ └── sgsxgmath │ │ ├── sgsxgmath.vcxproj │ │ └── sgsxgmath.vcxproj.filters ├── vc14 │ ├── SGScript.sln │ ├── SGScript │ │ ├── SGScript.vcxproj │ │ └── SGScript.vcxproj.filters │ ├── sgsapitest │ │ ├── sgsapitest.vcxproj │ │ └── sgsapitest.vcxproj.filters │ ├── sgsc │ │ ├── sgsc.vcxproj │ │ └── sgsc.vcxproj.filters │ ├── sgsjson │ │ ├── sgsjson.vcxproj │ │ └── sgsjson.vcxproj.filters │ ├── sgsmeta │ │ ├── sgsmeta.vcxproj │ │ └── sgsmeta.vcxproj.filters │ ├── sgspproc │ │ ├── sgspproc.vcxproj │ │ └── sgspproc.vcxproj.filters │ ├── sgssockets │ │ ├── sgssockets.vcxproj │ │ └── sgssockets.vcxproj.filters │ ├── sgstest │ │ ├── sgstest.vcxproj │ │ └── sgstest.vcxproj.filters │ ├── sgsvm │ │ ├── sgsvm.vcxproj │ │ └── sgsvm.vcxproj.filters │ └── sgsxgmath │ │ ├── sgsxgmath.vcxproj │ │ └── sgsxgmath.vcxproj.filters └── xcode │ └── SGScript.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── SGScript.xccheckout ├── core.mk ├── docs ├── advdoc.css ├── advdoc.js ├── docgen.sgs ├── docs.css ├── docs.js ├── error_policy.txt ├── info.svg ├── logo.svg ├── logo_bw.svg ├── markdown.sgs ├── sgs.cppbc.docs.htm ├── sgs.cppbc.docs.txt ├── sgs.json.docs.htm ├── sgs.json.docs.txt ├── sgs.meta.docs.htm ├── sgs.meta.docs.txt ├── sgs.sockets.docs.htm ├── sgs.sockets.docs.txt ├── sgs.xgmath.docs.htm ├── sgs.xgmath.docs.txt ├── sgscript.docs.htm ├── sgscript.docs.txt ├── sgscript.tutorial.htm └── sgscript.tutorial.txt ├── dotnet ├── APITest │ ├── APITest.cs │ ├── APITest.csproj │ ├── Properties │ │ └── AssemblyInfo.cs │ └── app.config ├── SGS.NET.sln ├── SGS.NET │ ├── Context.cs │ ├── NativeInterface.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── SGS.NET.csproj │ └── Variable.cs └── VM │ ├── Properties │ └── AssemblyInfo.cs │ ├── VM.cs │ ├── VM.csproj │ └── app.config ├── examples ├── AndroidSampleProject │ ├── AndroidManifest.xml │ ├── build.xml │ ├── jni │ │ ├── Android.mk │ │ └── interface.c │ └── src │ │ └── com │ │ └── sgscript │ │ └── sample │ │ └── Main.java ├── action-flow.sgs ├── benchmarksgame │ └── binarytrees.sgs ├── closure-counter.sgs ├── demodumps │ ├── fapi.sgs │ ├── fmt.sgs │ ├── misapi.sgs │ └── osapi.sgs ├── directory-size.sgs ├── hash-test.sgs ├── memory-test.sgs ├── opchain-test.sgs ├── pagination.sgs ├── recursion-test.sgs ├── sandbox.sgs ├── sgstest_mt.c ├── speed-dict.sgs ├── speed-map.sgs └── threaded-load.sgs ├── ext ├── cppbc.cmake ├── cppbc.sgs ├── sgs_cppbc.h ├── sgs_dbgserver.c ├── sgs_dbgserver.h ├── sgs_em.c ├── sgs_idbg.c ├── sgs_idbg.h ├── sgs_prof.c ├── sgs_prof.h ├── sgsapitest.c ├── sgsapitest_core.h ├── sgsc.c ├── sgscppbctest.cpp ├── sgscppbctest.h ├── sgsdemo.htm ├── sgsexe.c ├── sgsjson.c ├── sgsmeta.c ├── sgspproc.c ├── sgssockets.c ├── sgstest.c ├── sgsubench.c ├── sgsvm.c ├── sgsxgmath.c ├── sgsxgmath.h └── stubapp.bin ├── jni ├── Android.mk ├── Application.mk └── test_android.sgs ├── lib └── output.dir ├── makefile ├── obj └── output.dir ├── src ├── sgs_bcg.c ├── sgs_ctx.c ├── sgs_fnt.c ├── sgs_int.h ├── sgs_msvc_dirent.h ├── sgs_proc.c ├── sgs_regex.c ├── sgs_regex.h ├── sgs_srlz.c ├── sgs_std.c ├── sgs_stdL.c ├── sgs_tok.c ├── sgs_util.c ├── sgs_util.h ├── sgs_xpc.c └── sgscript.h └── tests ├── 100-syntax_core_MT.sgs ├── 125-boolops_TF.sgs ├── 150-functions_MT.sgs ├── 175-vararg_TF.sgs ├── 200-closure_MT.sgs ├── 225-runtime_MT.sgs ├── 250-syntax_TF.sgs ├── 275-defer_MT.sgs ├── 300-class_MT.sgs ├── 330-metamethods_TF.sgs ├── 360-class_TF.sgs ├── 400-threads_MT.sgs ├── 450-threading_TF.sgs ├── 500-misc_MT.sgs ├── 530-dmg_ctrl_TF.sgs ├── 560-bugs_MT.sgs ├── 600-std_TF.sgs ├── 650-type_TF.sgs ├── 675-sgson_TF.sgs ├── 700-string_TF.sgs ├── 730-fmt_TF.sgs ├── 760-io_TF.sgs ├── 800-json_TF.sgs ├── 830-meta_TF.sgs ├── 860-xgmath_TF.sgs ├── 900-perfreq_MT.sgs ├── data └── output.dir ├── f_cutwhile.sgs ├── monkey ├── monkey.sgs └── monkey_dict.txt ├── s_fnexp.sgs └── s_for.sgs /.gitignore: -------------------------------------------------------------------------------- 1 | *.depend 2 | *.layout 3 | /bin/* 4 | /obj/* 5 | /lib/* 6 | /tests/data/ 7 | *.idb 8 | *.pdb 9 | *.log 10 | *.suo 11 | *.opensdf 12 | *.sdf 13 | *~ 14 | ipch 15 | *.user 16 | !output.dir 17 | /docs/out/ 18 | /docs/adv/ 19 | /docs/*.toc 20 | .DS_Store 21 | xcuserdata 22 | /ext/sgscript.js 23 | /libs/ 24 | /examples/AndroidSampleProject/obj/ 25 | /examples/AndroidSampleProject/libs/ 26 | /examples/AndroidSampleProject/gen/ 27 | /examples/AndroidSampleProject/bin/ 28 | /dotnet/*/obj/ 29 | /dotnet/*/bin/ 30 | *.VC.* 31 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | os: 3 | - linux 4 | - osx 5 | compiler: 6 | - clang 7 | - gcc 8 | script: 9 | - 'make clean tools build_cppbctest' 10 | - 'bin/sgstest | tee ci_test.log && grep "/// Tests failed: 0 /" ci_test.log' 11 | - 'bin/sgsapitest -v | tee ci_apitest.log && grep "\[sgsapitest\] all tests were successful" ci_apitest.log' 12 | - 'bin/sgscppbctest | tee ci_cppbctest.log && grep "\[cppbc\] SUCCESS!" ci_cppbctest.log' 13 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required( VERSION 2.6 ) 3 | 4 | project( SGScript ) 5 | 6 | include( ext/cppbc.cmake ) 7 | set( SGSVM_PATH "${CMAKE_SOURCE_DIR}/bin/sgsvm" ) 8 | # set( SGSCPPBC_PATH "${CMAKE_SOURCE_DIR}/ext/cppbc.sgs" ) 9 | 10 | set( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin" ) 11 | set( CMAKE_SHARED_LIBRARY_PREFIX "" ) 12 | 13 | if(NOT MSVC) 14 | set(PF_CORE_FLAGS "-m32") 15 | set(CORE_FLAGS "${PF_CORE_FLAGS} -fwrapv -Wall -Wconversion -Wno-shadow -Wpointer-arith -Wcast-qual -Wcast-align -static-libgcc -Wno-missing-braces -g -fno-unwind-tables -ffunction-sections -I${CMAKE_SOURCE_DIR}/src") 16 | set(CXX_CORE_FLAGS "${CORE_FLAGS} -fno-rtti -fno-exceptions") 17 | if(NOT WIN32) 18 | set(CORE_FLAGS "${CORE_FLAGS} -fPIC") 19 | endif(NOT WIN32) 20 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CORE_FLAGS}") 21 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_CORE_FLAGS}") 22 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CXX_CORE_FLAGS}") 23 | set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${CXX_CORE_FLAGS}") 24 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CXX_CORE_FLAGS}") 25 | else() 26 | set(CORE_FLAGS "") 27 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR- /W3 /Zi /wd4244 /wd4996") 28 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG") 29 | set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /DEBUG") 30 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG") 31 | endif() 32 | 33 | set( SGSCRIPT_FILES 34 | src/sgs_bcg.c 35 | src/sgs_ctx.c 36 | src/sgs_fnt.c 37 | src/sgs_proc.c 38 | src/sgs_regex.c 39 | src/sgs_srlz.c 40 | src/sgs_std.c 41 | src/sgs_stdL.c 42 | src/sgs_tok.c 43 | src/sgs_util.c 44 | src/sgs_xpc.c 45 | ) 46 | add_library( sgscript SHARED ${SGSCRIPT_FILES} ) 47 | 48 | # SGSVM 49 | add_executable( sgsvm ext/sgsvm.c ext/sgs_prof.c ext/sgs_idbg.c ) 50 | target_include_directories( sgsvm PRIVATE src ) 51 | target_link_libraries( sgsvm sgscript ) 52 | 53 | # SGS/CPP-BC test 54 | add_executable( sgscppbctest ext/sgscppbctest.cpp obj/cppbc_test.cpp ) 55 | add_dependencies( sgscppbctest sgsvm ) 56 | target_include_directories( sgscppbctest PRIVATE src ) 57 | cppbc_header( sgscppbctest obj/cppbc_test.cpp ext/sgscppbctest.h ../ext/sgscppbctest.h ) 58 | target_link_libraries( sgscppbctest sgscript ) 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2016 Arvīds Kokins 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SGScript v1.4.1 2 | 3 | ## Usage 4 | 5 | - MinGW/GNU Make/GCC/Clang users: 6 | * compile the makefile (add ```mode=release``` to get the release build) 7 | * include ```src/sgscript.h``` and link with -lsgscript from the bin/ directory (-Lbin) 8 | * to get a static library, use ```static=1``` and link to ```lib/libsgscript.a``` (-Llib -lsgscript) 9 | - VC10+ users: project file is in build/vc10/SGScript 10 | - XCode users: project file is in build/xcode 11 | - Android NDK users: include jni/Android.mk into your makefile 12 | 13 | ## Sample Code and Documentation 14 | 15 | Look in ```examples/```, ```tests/``` and ```docs/``` directories. 16 | 17 | To build local HTML5 documentation, use "make docs". 18 | 19 | More sample code can be found in documentation: http://www.sgscript.org/docs/sgscript.docs/code-samples-sgscript 20 | 21 | ## Features 22 | 23 | - A C-like syntax 24 | - The usual stuff (while/do-while/for/foreach, expressions, variables etc.) 25 | - Highly optimized, register-based virtual machine 26 | - Mixed memory management (ref.counting + GC) 27 | - Extensive native debugging features 28 | - **Coroutines, threads, advanced sync features** 29 | - **Interactive debug inspector add-on** 30 | - **Function/instruction execution time and memory usage profiler add-on** 31 | - 10 data types (with lots of space for extensions): 32 | * null, bool, int, real, string, function, C function, object, pointer, thread 33 | - Tests: 34 | * testing framework is in ext/sgstest.c => bin/sgstest ("make test" to run) 35 | * API testing framework is in ext/sgsapitest.c => bin/sgsapitest ("make apitest" to run) 36 | * C++/BC testing framework is in ext/sgscppbctest.cpp/h => bin/sgscppbctest ("make cppbctest" or "make cppbctest11" to run) 37 | - Object-oriented constructs (dict, class, closure, "compatible call", overloadable operators) 38 | 39 | ## Bindings 40 | 41 | - .NET 2.0+ bindings are at ```dotnet/SGS.NET``` (currently only tested on Windows - standalone VM and Unity) 42 | 43 | ## Bugs 44 | 45 | - Development branch status: 46 | [![Travis CI Build Status](https://travis-ci.org/snake5/sgscript.svg?branch=apidev)](https://travis-ci.org/snake5/sgscript) 47 | [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/snake5/sgscript?svg=true&branch=apidev)](https://ci.appveyor.com/project/snake5/sgscript) 48 | 49 | If you think you've found a bug, please create a new issue on GitHub. 50 | 51 | Don't forget to include a test sample, as small as possible! 52 | 53 | ## Future plans 54 | 55 | - full state serialization 56 | - got a suggestion? write some sample code (in the form of a test) and send it here 57 | 58 | ## Community 59 | 60 | - [SGScript on Discord](https://discord.gg/QysXUNq) 61 | 62 | ## Credits 63 | 64 | - developer: Arvīds Kokins (snake5) 65 | * I can be reached at https://twitter.com/snake5creator and snake5creator [at] GMail 66 | 67 | -------------------------------------------------------------------------------- /bin/output.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archo5/sgscript/980a513db16b63ce65bd84c6f5084e4af7376379/bin/output.dir -------------------------------------------------------------------------------- /build/hgen.sgs: -------------------------------------------------------------------------------- 1 | 2 | // libraries 3 | include "io", "re", "string"; 4 | 5 | // configuration 6 | global OUT = null; 7 | global MICRO = false; 8 | 9 | global argv; 10 | if( @argv ) 11 | { 12 | for( i = 0; i < argv.size; ++i ) 13 | { 14 | arg = argv[i]; 15 | if( arg == "-m" || arg == "--micro" ) MICRO = true; 16 | if( arg == "-o" || arg == "--out" ) OUT = @argv[++i]; 17 | } 18 | } 19 | 20 | if( !OUT ) 21 | { 22 | errprintln( "SGS header generator - no output file (-o) specified" ); 23 | return; 24 | } 25 | 26 | orig_io_file_read = io_file_read; 27 | function io_file_read( src ) use( orig_io_file_read ) 28 | { 29 | println( "reading " $ src ); 30 | return orig_io_file_read( src ); 31 | } 32 | orig_io_file_write = io_file_write; 33 | function io_file_write( src, data ) use( orig_io_file_write ) 34 | { 35 | println( "writing "$data.length$" bytes to " $ src ); 36 | return orig_io_file_write( src, data ); 37 | } 38 | 39 | // concatenate source files 40 | SRC = sys_curfiledir() $ "/../src/"; 41 | FILES = 42 | [ 43 | "sgs_bcg.c", 44 | "sgs_ctx.c", 45 | "sgs_fnt.c", 46 | "sgs_proc.c", 47 | "sgs_regex.c", 48 | "sgs_srlz.c", 49 | "sgs_std.c", 50 | "sgs_util.c", 51 | "sgs_tok.c", 52 | "sgs_xpc.c", 53 | ]; 54 | REPLACED = []; 55 | NL = "\n"; 56 | BLOB = NL$"#define SGS_INTERNAL_STRINGTABLES"$NL; 57 | if( !MICRO ) 58 | { 59 | BLOB $= "#define SGS_USE_FILESYSTEM"$NL; 60 | FILES.push( "sgs_stdL.c" ); 61 | } 62 | else 63 | { 64 | BLOB $= "#define SGS_NO_STDLIB 1"$NL; 65 | REPLACED.push( "sgs_msvc_dirent.h" ); 66 | } 67 | 68 | foreach( file : FILES ) 69 | BLOB $= io_file_read( SRC $ file ); 70 | 71 | // replace local includes (only the first, just remove repeated ones) 72 | 73 | for(;;) 74 | { 75 | incl = re_match( BLOB, "/#[ \t]*include[ \t]*[\"<](sgs[a-z0-9_.]+)[>\"]/i", RE_RETURN_BOTH ); 76 | if( !incl ) 77 | break; 78 | 79 | file = incl[1][0]; 80 | from = incl[0][1]; 81 | to = incl[0][2]; 82 | 83 | // printvar( file, from, to ); 84 | if( REPLACED.find( file ) !== null ) 85 | { 86 | BLOB = string_part( BLOB, 0, from ) $ string_part( BLOB, to ); 87 | } 88 | else 89 | { 90 | incfile = io_file_read( SRC $ file ); 91 | BLOB = string_part( BLOB, 0, from ) $ incfile $ string_part( BLOB, to ); 92 | REPLACED.push( file ); 93 | } 94 | } 95 | 96 | io_file_write( OUT, BLOB ); 97 | -------------------------------------------------------------------------------- /build/prep.sgs: -------------------------------------------------------------------------------- 1 | 2 | // Must be run with root as the current directory 3 | 4 | include "io", "os", "string", "re"; 5 | 6 | 7 | if( !@get_make_cmd ) 8 | { 9 | function get_make_cmd( os ) 10 | { 11 | cmd = "clean_objbin tools"; 12 | if( os == "windows" ) 13 | cmd ..= " sgsexe"; 14 | return cmd; 15 | } 16 | } 17 | if( !@get_clean_cmd ) 18 | { 19 | function get_clean_cmd( os ) 20 | { 21 | return "clean_obj"; 22 | } 23 | } 24 | if( !@get_buildstrip_cmd ) 25 | { 26 | function get_buildstrip_cmd(){} 27 | } 28 | if( !@get_version_text ) 29 | { 30 | function get_version_text() 31 | { 32 | mainheader = io_file_read( "src/sgscript.h" ); 33 | match = re_match( mainheader, "%major[ \t]+([0-9]+).*?minor[ \t]+([0-9]+).*?" .. 34 | "incr[ \t]+([0-9]+)%msi", RE_RETURN_CAPTURED ); 35 | return string_format( '{1}.{2}.{3}', match[1], match[2], match[3] ); 36 | } 37 | } 38 | 39 | 40 | // OS config 41 | global OSNAMES_TR = { "mac os x" = "osx" }; 42 | global OSNAME = string_tolower( os_gettype() ); 43 | global OSNAME = @OSNAMES_TR[ OSNAME ] || OSNAME; 44 | global DS = if( OSNAME == "windows", "\\", "/" ); // only necessary for os_command 45 | global ROOT = "bin" .. DS; 46 | if( OSNAME == "windows" ) 47 | { 48 | // .ZIP 49 | function os_make_archive( archname, folder ) 50 | { 51 | locations_7z = 52 | [ 53 | """C:\Program Files\7-Zip\7z.exe""", 54 | """C:\Program Files (x86)\7-Zip\7z.exe""" 55 | ]; 56 | done = false; 57 | foreach( bin7z : locations_7z ) 58 | { 59 | if( !io_file_exists( bin7z ) ) 60 | continue; 61 | 62 | // try to run 7-zip 63 | os_command( string_format( '"{1}" a -mx9 -tzip {2}.zip {3}', bin7z, archname, folder ) ); 64 | 65 | done = true; 66 | break; 67 | } 68 | if( !done ) 69 | ERROR( "could not find 7-Zip" ); 70 | } 71 | } 72 | else if( OSNAME == "osx" ) 73 | { 74 | // .ZIP 75 | function os_make_archive( archname, folder ) 76 | { 77 | os_command( string_format( "zip -r -9 -X {1}.zip {2}", archname, folder ) ); 78 | } 79 | } 80 | else 81 | { 82 | // .TAR.GZ 83 | function os_make_archive( archname, folder ) 84 | { 85 | os_command( string_format( "tar -zcvf {1}.tar.gz {2}", archname, folder ) ); 86 | } 87 | } 88 | 89 | // build variation config 90 | modes = [ "debug", "release" ]; 91 | arches = [ "x86", "x64" ]; 92 | 93 | // build command config 94 | CPBIN = if( OSNAME == "windows", "copy /b", "cp" ); 95 | RMBIN = if( OSNAME == "windows", "del /F /S /Q", "rm -rf" ); 96 | MAKEBIN = if( OSNAME == "windows", "mingw32-make", "make" ); 97 | MAKECMD = get_make_cmd( OSNAME ); 98 | 99 | // -------------------------------------------- 100 | 101 | versiontext = get_version_text(); 102 | 103 | buildlist = {}; 104 | 105 | foreach( arch : arches ) 106 | foreach( mode : modes ) 107 | { 108 | folder = "sgscript-" .. versiontext .. "-" .. OSNAME .. "-" .. arch .. "-" .. mode; 109 | buildlist[ folder ] = "mode=" .. mode .. " arch=" .. arch; 110 | } 111 | 112 | cwd = io_getcwd(); 113 | foreach( blfolder, blflags : buildlist ) 114 | { 115 | blflags = string_trim( blflags ); 116 | printlns 117 | ( 118 | "", 119 | "", 120 | "//////////////////////////////", 121 | "// build type: " .. blfolder, 122 | "// build flags: " .. blflags, 123 | "//////////////////////////////", 124 | "" 125 | ); 126 | 127 | // build 128 | DIR = ROOT .. blfolder; 129 | io_dir_create( DIR ); 130 | os_command( MAKEBIN .. " OUTDIR=" .. DIR .. " " .. blflags .. " " .. MAKECMD ); 131 | os_command( CPBIN .. " src" .. DS .. "sgscript.h " .. DIR .. DS .. "sgscript.h" ); 132 | os_command( CPBIN .. " ext" .. DS .. "sgsxgmath.h " .. DIR .. DS .. "sgsxgmath.h" ); 133 | os_command( CPBIN .. " ext" .. DS .. "sgs_cppbc.h " .. DIR .. DS .. "sgs_cppbc.h" ); 134 | os_command( CPBIN .. " ext" .. DS .. "cppbc.sgs " .. DIR .. DS .. "cppbc.sgs" ); 135 | os_command( CPBIN .. " ext" .. DS .. "cppbc.cmake " .. DIR .. DS .. "cppbc.cmake" ); 136 | rmpaths = get_buildstrip_cmd( DIR, blflags ); 137 | if( rmpaths !== null ) 138 | { 139 | if( OSNAME == "windows" ) 140 | rmpaths = string_replace( rmpaths, "/", "\\" ); 141 | os_command( RMBIN .. " " .. rmpaths ); 142 | } 143 | 144 | // generate archive 145 | io_setcwd( cwd .. "/bin" ); 146 | os_make_archive( blfolder, blfolder ); 147 | io_setcwd( cwd ); 148 | 149 | // write version info 150 | // io_file_write( DIR .. "/version", versioninfo_plain ); 151 | // io_file_write( DIR .. "/version.json", versioninfo_json ); 152 | } 153 | os_command( MAKEBIN .. " " .. get_clean_cmd( OSNAME ) ); 154 | -------------------------------------------------------------------------------- /build/vc10/SGScript/SGScript.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /build/vc10/sgsapitest/sgsapitest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /build/vc10/sgsc/sgsc.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc10/sgsjson/sgsjson.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {5502D47D-7A93-41C5-A005-E9F55E9A91C8} 15 | Win32Proj 16 | sgsjson 17 | sgsjson (JSON library) 18 | 19 | 20 | 21 | DynamicLibrary 22 | true 23 | NotSet 24 | 25 | 26 | DynamicLibrary 27 | false 28 | true 29 | NotSet 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | $(ProjectDir)..\..\..\obj\sgsjson-$(Configuration)\ 44 | $(ProjectDir)..\..\..\bin\ 45 | sgsjson 46 | 47 | 48 | false 49 | $(ProjectDir)..\..\..\obj\sgsjson-$(Configuration)\ 50 | $(ProjectDir)..\..\..\bin\ 51 | sgsjson 52 | 53 | 54 | 55 | 56 | 57 | Level3 58 | Disabled 59 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;SGS_DLL;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) 60 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 61 | false 62 | CompileAsCpp 63 | 64 | 65 | Windows 66 | true 67 | sgscript.lib;%(AdditionalDependencies) 68 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 69 | 70 | 71 | 72 | 73 | Level3 74 | 75 | 76 | MaxSpeed 77 | false 78 | true 79 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;SGS_DLL;_WINDOWS;NDEBUG;%(PreprocessorDefinitions) 80 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 81 | false 82 | CompileAsCpp 83 | true 84 | false 85 | 86 | 87 | Windows 88 | true 89 | true 90 | true 91 | sgscript.lib;%(AdditionalDependencies) 92 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /build/vc10/sgsjson/sgsjson.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc10/sgsmeta/sgsmeta.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {850CF552-2D58-4266-9CFF-34F95743895E} 15 | Win32Proj 16 | sgsmeta 17 | sgsmeta (Language utility library) 18 | 19 | 20 | 21 | DynamicLibrary 22 | true 23 | NotSet 24 | 25 | 26 | DynamicLibrary 27 | false 28 | true 29 | NotSet 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | $(ProjectDir)..\..\..\obj\sgsmeta-$(Configuration)\ 44 | $(ProjectDir)..\..\..\bin\ 45 | sgsmeta 46 | 47 | 48 | false 49 | $(ProjectDir)..\..\..\obj\sgsmeta-$(Configuration)\ 50 | $(ProjectDir)..\..\..\bin\ 51 | sgsmeta 52 | 53 | 54 | 55 | 56 | 57 | Level3 58 | Disabled 59 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) 60 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 61 | false 62 | CompileAsCpp 63 | 64 | 65 | Windows 66 | true 67 | sgscript.lib;%(AdditionalDependencies) 68 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 69 | 70 | 71 | 72 | 73 | Level3 74 | 75 | 76 | MaxSpeed 77 | false 78 | true 79 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;_WINDOWS;NDEBUG;%(PreprocessorDefinitions) 80 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 81 | false 82 | CompileAsCpp 83 | true 84 | false 85 | 86 | 87 | Windows 88 | true 89 | true 90 | true 91 | sgscript.lib;%(AdditionalDependencies) 92 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /build/vc10/sgsmeta/sgsmeta.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc10/sgspproc/sgspproc.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {FC3DCBA1-6B29-4286-8E42-F41A46516CE4} 15 | Win32Proj 16 | sgspproc 17 | sgspproc (Parallel processing library) 18 | 19 | 20 | 21 | DynamicLibrary 22 | true 23 | NotSet 24 | 25 | 26 | DynamicLibrary 27 | false 28 | true 29 | NotSet 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | $(ProjectDir)..\..\..\obj\sgspproc-$(Configuration)\ 44 | $(ProjectDir)..\..\..\bin\ 45 | sgspproc 46 | 47 | 48 | false 49 | $(ProjectDir)..\..\..\obj\sgspproc-$(Configuration)\ 50 | $(ProjectDir)..\..\..\bin\ 51 | sgspproc 52 | 53 | 54 | 55 | 56 | 57 | Level3 58 | Disabled 59 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) 60 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 61 | false 62 | CompileAsCpp 63 | 64 | 65 | Windows 66 | true 67 | sgscript.lib;%(AdditionalDependencies) 68 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 69 | 70 | 71 | 72 | 73 | Level3 74 | 75 | 76 | MaxSpeed 77 | false 78 | true 79 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;_WINDOWS;NDEBUG;%(PreprocessorDefinitions) 80 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 81 | false 82 | CompileAsCpp 83 | true 84 | false 85 | 86 | 87 | Windows 88 | true 89 | true 90 | true 91 | sgscript.lib;%(AdditionalDependencies) 92 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /build/vc10/sgspproc/sgspproc.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc10/sgssockets/sgssockets.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {E57FD86B-3796-4B64-AFD5-C3DCDF6B8522} 15 | Win32Proj 16 | sgssockets 17 | sgssockets (Network socket library) 18 | 19 | 20 | 21 | DynamicLibrary 22 | true 23 | NotSet 24 | 25 | 26 | DynamicLibrary 27 | false 28 | true 29 | NotSet 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | $(ProjectDir)..\..\..\obj\sgssockets-$(Configuration)\ 44 | $(ProjectDir)..\..\..\bin\ 45 | sgssockets 46 | 47 | 48 | false 49 | $(ProjectDir)..\..\..\obj\sgssockets-$(Configuration)\ 50 | $(ProjectDir)..\..\..\bin\ 51 | sgssockets 52 | 53 | 54 | 55 | 56 | 57 | Level3 58 | Disabled 59 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) 60 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 61 | false 62 | CompileAsCpp 63 | 64 | 65 | Windows 66 | true 67 | sgscript.lib;ws2_32.lib;%(AdditionalDependencies) 68 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 69 | 70 | 71 | 72 | 73 | Level3 74 | 75 | 76 | MaxSpeed 77 | false 78 | true 79 | WIN32;SGS_DLL;SGS_COMPILE_MODULE;_WINDOWS;NDEBUG;%(PreprocessorDefinitions) 80 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 81 | false 82 | CompileAsCpp 83 | true 84 | false 85 | 86 | 87 | Windows 88 | true 89 | true 90 | true 91 | sgscript.lib;ws2_32.lib;%(AdditionalDependencies) 92 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /build/vc10/sgssockets/sgssockets.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc10/sgstest/sgstest.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | 16 | 17 | {A3DC82EA-F984-4E7E-8FBA-1C236F2815D5} 18 | Win32Proj 19 | sgstest 20 | sgstest (Test Tool) 21 | 22 | 23 | 24 | Application 25 | true 26 | NotSet 27 | 28 | 29 | Application 30 | false 31 | true 32 | NotSet 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | true 46 | $(ProjectDir)..\..\..\bin\ 47 | $(ProjectDir)..\..\..\obj\sgstest-$(Configuration)\ 48 | sgstest 49 | 50 | 51 | false 52 | $(ProjectDir)..\..\..\bin\ 53 | $(ProjectDir)..\..\..\obj\sgstest-$(Configuration)\ 54 | sgstest 55 | 56 | 57 | 58 | 59 | 60 | Level3 61 | Disabled 62 | WIN32;SGS_DLL;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 63 | false 64 | CompileAsCpp 65 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 66 | 67 | 68 | Console 69 | true 70 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 71 | sgscript.lib;%(AdditionalDependencies) 72 | 73 | 74 | 75 | 76 | Level3 77 | 78 | 79 | MaxSpeed 80 | true 81 | true 82 | WIN32;SGS_DLL;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 83 | false 84 | CompileAsCpp 85 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 86 | MultiThreadedDLL 87 | 88 | 89 | Console 90 | true 91 | true 92 | true 93 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 94 | sgscript.lib;%(AdditionalDependencies) 95 | msvcrtd;LIBCMT;LIBCMTD 96 | 97 | 98 | 99 | 100 | 101 | -------------------------------------------------------------------------------- /build/vc10/sgstest/sgstest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc10/sgsvm/sgsvm.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build/vc10/sgsxgmath/sgsxgmath.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {CDFD2448-51C8-4402-8061-71CEA1D5AF3A} 15 | Win32Proj 16 | sgsxgmath 17 | sgsxgmath (eXtended Game Math library) 18 | 19 | 20 | 21 | DynamicLibrary 22 | true 23 | NotSet 24 | 25 | 26 | DynamicLibrary 27 | false 28 | true 29 | NotSet 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | true 43 | $(ProjectDir)..\..\..\obj\sgsxgmath-$(Configuration)\ 44 | $(ProjectDir)..\..\..\bin\ 45 | sgsxgmath 46 | 47 | 48 | false 49 | $(ProjectDir)..\..\..\obj\sgsxgmath-$(Configuration)\ 50 | $(ProjectDir)..\..\..\bin\ 51 | sgsxgmath 52 | 53 | 54 | 55 | 56 | 57 | Level3 58 | Disabled 59 | WIN32;SGS_COMPILE_MODULE;SGS_DLL;_WINDOWS;_DEBUG;%(PreprocessorDefinitions) 60 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 61 | false 62 | CompileAsCpp 63 | 64 | 65 | Windows 66 | true 67 | sgscript.lib;%(AdditionalDependencies) 68 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 69 | 70 | 71 | 72 | 73 | Level3 74 | 75 | 76 | MaxSpeed 77 | false 78 | true 79 | WIN32;SGS_COMPILE_MODULE;SGS_DLL;_WINDOWS;NDEBUG;%(PreprocessorDefinitions) 80 | $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) 81 | false 82 | CompileAsCpp 83 | true 84 | false 85 | 86 | 87 | Windows 88 | true 89 | true 90 | true 91 | sgscript.lib;%(AdditionalDependencies) 92 | $(SolutionDir)..\..\bin;%(AdditionalLibraryDirectories) 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /build/vc10/sgsxgmath/sgsxgmath.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build/vc14/SGScript/SGScript.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /build/vc14/sgsapitest/sgsapitest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /build/vc14/sgsc/sgsc.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc14/sgsjson/sgsjson.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc14/sgsmeta/sgsmeta.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc14/sgspproc/sgspproc.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc14/sgssockets/sgssockets.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc14/sgstest/sgstest.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /build/vc14/sgsvm/sgsvm.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build/vc14/sgsxgmath/sgsxgmath.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /build/xcode/SGScript.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /build/xcode/SGScript.xcodeproj/project.xcworkspace/xcshareddata/SGScript.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | D88631B9-2BA5-4926-864A-C888971EBBE1 9 | IDESourceControlProjectName 10 | SGScript 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | FF6F4422-5176-4CF6-AFF5-649E6B296CBD 14 | https://github.com/snake5/sgscript.git 15 | 16 | IDESourceControlProjectPath 17 | build/xcode/SGScript.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | FF6F4422-5176-4CF6-AFF5-649E6B296CBD 21 | ../../../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/snake5/sgscript.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | FF6F4422-5176-4CF6-AFF5-649E6B296CBD 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | FF6F4422-5176-4CF6-AFF5-649E6B296CBD 36 | IDESourceControlWCCName 37 | sgscript 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /core.mk: -------------------------------------------------------------------------------- 1 | 2 | # MAKE 3 | comma := , 4 | space := 5 | space += 6 | 7 | 8 | # BASIC VARIABLES 9 | ifeq ($(CC),cc) # no such compiler on Windows 10 | CC= 11 | endif 12 | ifeq ($(CC),) 13 | CC=gcc 14 | endif 15 | ifeq ($(CXX),) 16 | CXX=g++ 17 | endif 18 | ifeq ($(OUTDIR),) 19 | OUTDIR=bin 20 | endif 21 | 22 | 23 | # UTILITIES 24 | cOS=os_unknown 25 | cARCH=arch_unknown 26 | cCOMPILER=compiler_unknown 27 | ifeq ($(OS),Windows_NT) 28 | fnREMOVE_ALL = del /F /S /Q 29 | fnCOPY_FILE = copy 30 | fnFIX_PATH = $(subst /,\,$1) 31 | cOS=windows 32 | ifeq ($(PROCESSOR_ARCHITECTURE),x86) 33 | cARCH=x86 34 | endif 35 | ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) 36 | cARCH=x64 37 | endif 38 | else 39 | fnREMOVE_ALL = rm -rf 40 | fnCOPY_FILE = cp -f 41 | fnFIX_PATH = $1 42 | UNAME_S := $(shell uname -s) 43 | ifeq ($(UNAME_S),Linux) 44 | cOS=linux 45 | endif 46 | ifeq ($(UNAME_S),Darwin) 47 | cOS=osx 48 | endif 49 | UNAME_M := $(shell uname -m) 50 | ifneq ($(filter %86,$(UNAME_M)),) 51 | cARCH=x86 52 | endif 53 | ifeq ($(UNAME_M),x86_64) 54 | cARCH=x64 55 | endif 56 | ifneq ($(filter arm%,$(UNAME_M)),) 57 | cARCH=arm 58 | endif 59 | endif 60 | CC_V := $(shell $(CC) 2>&1) 61 | ifneq ($(findstring clang,$(CC_V)),) 62 | cCOMPILER=clang 63 | endif 64 | ifneq ($(findstring gcc,$(CC_V)),) 65 | cCOMPILER=gcc 66 | endif 67 | 68 | 69 | ifeq ($(os),) 70 | os=$(cOS) 71 | endif 72 | ifeq ($(arch),) 73 | arch=$(cARCH) 74 | endif 75 | cIF_RELEASE=$(findstring release,$(mode)) 76 | fnIF_RELEASE=$(if $(cIF_RELEASE),$1,$2) 77 | fnIF_OS=$(if $(findstring $1,$(os)),$2,$3) 78 | fnIF_ARCH=$(if $(findstring $1,$(arch)),$2,$3) 79 | fnIF_OS_ARCH=$(if $(findstring $1,$(target)),$2,$3) 80 | fnIF_COMPILER=$(if $(findstring $1,$(cCOMPILER)),$2,$3) 81 | 82 | 83 | # PLATFORM SPECIFICS 84 | ifeq ($(os),windows) 85 | BINEXT=.exe 86 | LIBPFX= 87 | LIBEXT=.dll 88 | else 89 | BINEXT= 90 | LIBPFX=lib 91 | LIBEXT=.so 92 | endif 93 | -------------------------------------------------------------------------------- /docs/advdoc.css: -------------------------------------------------------------------------------- 1 | 2 | mark { background: #FDB; padding: 2px 4px; } 3 | html { height: 100%; } 4 | body { padding: 0; height: 100%; } 5 | introtext { display: block; margin: 32px; font-size: 24px; } 6 | #_frame_ { height: 100%; } 7 | docframe { display: flex; flex-direction: row; min-height: 100%; max-height: 100%; } 8 | toc { display: flex; flex-direction: column; min-height: 100%; box-sizing: border-box; padding: 12px 12px 0 12px; 9 | width: 300px; border-right: 2px solid #BBB; position: absolute; top: 0; left: 0; bottom: 0; } 10 | toc logo { display: block; margin: -12px 0 0 -4px; height: 80px; } 11 | toc header { margin: 0 0 12px 0; } 12 | toc header subtitle { display: block; background: #333; padding: 8px; color: #EEE; 13 | font-family: Verdana, sans-serif; border-radius: 3px; font-size: 12px; } 14 | toc header search { display: block; margin: 12px 0 0 0; border: 1px solid #999; } 15 | toc header search input { width: 100%; box-sizing: border-box; border: 1px solid #EEE; background: #DDD; padding: 2px 4px; } 16 | toc header search input::-webkit-input-placeholder { color: #777; opacity: 1; } 17 | toc header search input:-moz-placeholder { color: #777; opacity: 1; } 18 | toc header search input::-moz-placeholder { color: #777; opacity: 1; } 19 | toc header search input:-ms-input-placeholder { color: #777; opacity: 1; } 20 | toc header search input:placeholder-shown { color: #777; opacity: 1; } 21 | toc cont { position: relative; flex: auto; box-shadow: inset -2px 1px 2px rgba(0,0,0,0.2); } 22 | toc cont entrylist { position: absolute; overflow-y: scroll; top: 0; left: 0; right: 0; bottom: 0; padding: 2px 0; } 23 | entryset { display: block; } 24 | entrych { display: block; padding: 0 0 0 16px; } 25 | toc cont entry { display: block; padding: 2px 4px; cursor: pointer; border-radius: 3px; font-size: 14px; } 26 | toc cont entry:hover { background: rgba(254,124,4,0.5); color: #FFF; box-shadow: 0 0 5px rgba(254,124,4,0.5); } 27 | toc entry.active { background: rgba(200,8,4,0.5); color: #FFF; } 28 | toc entry.active:hover { background: rgba(228,66,4,0.5); } 29 | view { display: block; padding: 12px; flex: 1; overflow-y: auto; position: absolute; left: 300px; top: 0; right: 0; bottom: 0; } 30 | view ptitle { display: block; font-family: Verdana, sans-serif; font-weight: bold; font-size: 24px; } 31 | view searchresult { display: block; margin: 24px 0; } 32 | view searchresult srttl { display: block; margin: 8px 0; } 33 | view searchresult .title { font-size: 18px; } 34 | view searchresult desc { display: block; } 35 | view dbglog { display: block; } 36 | view dbglog:nth-child(odd) { background: #EED; } 37 | view dbglog:nth-child(even) { background: #DEE; } 38 | breadcrumbs { display: block; padding: 8px; margin: 0 0 16px 0; font-size: 14px; opacity: 0.7; 39 | border: 1px solid #EEE; outline: 1px solid #AAA; background: #DDD; } 40 | breadcrumbs:hover { opacity: 1; } 41 | breadcrumbs .sep { padding: 0 10px; } 42 | relatedlinks content { display: block; padding: 8px; margin: 32px 0 0 0; font-size: 14px; 43 | border: 1px solid #EEE; outline: 1px solid #AAA; background: #DDD; } 44 | relatedlinks content subtitle { font-family: Verdana, sans-serif; } 45 | relatedlinks content ul { margin: 4px 0 0 0; padding: 0 0 0 16px; } 46 | -------------------------------------------------------------------------------- /docs/docs.css: -------------------------------------------------------------------------------- 1 | 2 | body { font-family: Arial, Helvetica, sans-serif; margin: 0; padding: 16px; background: #EFEFEF; color: #333; } 3 | h1 { margin: 6px 0; background: #555; color: #EEE; padding: 2px 8px; } 4 | h2 { margin: 21px 0 5px 0; display: inline-block; background: #832; color: #EEE; padding: 2px 8px; } 5 | h3 { margin: 19px 0 4px 0; } 6 | p, ul { margin: 8px 0; line-height: 150%; } 7 | h1 a, h2 a, h3 a { color: inherit; } 8 | a { text-decoration: none; color: #832; } 9 | a:hover { text-decoration: underline; } 10 | .cont { margin-left: 16px; } 11 | .txt { line-height: 150%; } 12 | .toc { margin: 4px 16px; line-height: 130%; } 13 | .break { height: 16px; } 14 | .l1 { margin-left: 0px; } 15 | .l2 { margin-left: 16px; } 16 | table { box-shadow: 1px 1px 5px rgba( 0, 0, 0, 0.2 ); } 17 | th, td { padding: 2px 5px; } 18 | th { background: #FFF; } 19 | pre, 20 | code { background: #E5E5E5; display: inline-block; padding: 2px 4px; border-radius: 2px; line-height: 120%; border: 1px solid #F7F7F7; position: relative; } 21 | code { white-space: nowrap; } 22 | pre { tab-size: 4; } 23 | body pre { display: block; } 24 | code i { display: none; padding: 4px 8px; left: 120%; top: 50%; border: 1px solid #A00; background: #C21; color: #EEE; } 25 | code:hover i { display: inline-block; position: absolute; z-index: 2; border-radius: 5px; box-shadow: 2px 2px 5px rgba(0,0,0,0.5); } 26 | .notice-bgr { background: #BCE; padding: 8px; margin: 8px; display: block; } 27 | .notice { background: #79C; color: #FFF; font-weight: bold; padding: 4px; margin: 4px 8px 4px 4px; display: inline-block; font-size: 16px; border: 1px solid #68B; 28 | width: 20px; height: 20px; text-align: center; text-shadow: 0px 0px 5px #FFF, 0px 0px 9px #FFF, 0px 0px 13px #FFF; font-family: monospace; border-radius: 1px; } 29 | i.note { color: #AAA; font-size: 13px; } 30 | .al-pro .inner { color: #006400; } 31 | .al-con .inner { color: #b22222; } 32 | .al-pro .inner, .al-con .inner { display: inline-block; padding: 2px 0; } 33 | .al-pro .mark, .al-con .mark { padding: 2px 2px 2px 0; } 34 | dt { font-weight: bold; } 35 | 36 | .sgsCOM, pre > .sgsCOM *, code > .sgsCOM * { color: #5A3; } 37 | .sgsSTR, pre > .sgsSTR *, code > .sgsSTR * { color: #C21; } 38 | .sgsKEY { color: #13C; } 39 | .sgsSPC { color: #341; font-weight: bold; } 40 | .sgsNUM { color: #481; } 41 | pre a, code a { text-decoration: underline; } 42 | -------------------------------------------------------------------------------- /docs/docs.js: -------------------------------------------------------------------------------- 1 | 2 | function sgsCC( el ) 3 | { 4 | var code = el.innerHTML; 5 | code += "\n"; 6 | 7 | code = code.replace( """, "\uE000" ); 8 | code = code.replace( "'", "\uE001" ); 9 | 10 | code = code.replace( /(\uE000(?:[^\uE000\\]|\\.)*\uE000)/g, '$1' ); 11 | code = code.replace( /(\uE001(?:[^\uE001\\]|\\.)*\uE001)/g, '$1' ); 12 | code = code.replace( /(^|[^0-9a-zA-Z_])(0b[0-1]+)/g, '$1$2' ); 13 | code = code.replace( /(^|[^0-9a-zA-Z_])(0o[0-8]+)/g, '$1$2' ); 14 | code = code.replace( /(^|[^0-9a-zA-Z_])(0x[0-9a-fA-F]+)/g, '$1$2' ); 15 | code = code.replace( /(^|[^0-9a-zA-Z_])(-?[0-9]+(\.[0-9]+)?([eE][-+][0-9]+)?)/g, '$1$2' ); 16 | code = code.replace( /(^|[^0-9a-zA-Z_])(if|else|do|while|for|foreach|break|continue|var|global|function|return|null|true|false|use)(\s|\b)/g, '$1$2$3' ); 17 | code = code.replace( /(\(|\)|\[|\]|\{|\})/g, '$1' ); 18 | code = code.replace( /(\/\/[^\n\r]*)([\r\n])/g, '$1$2' ); 19 | code = code.replace( /(\/\*[\s\S]*?\*\/)/g, '$1' ); 20 | 21 | code = code.replace( "\uE000", """ ); 22 | code = code.replace( "\uE001", "'" ); 23 | 24 | code = code.replace(/^\s+|\s+$/g, ''); 25 | el.innerHTML = code; 26 | } 27 | 28 | function sgsCCify( tag ) 29 | { 30 | var codes = document.getElementsByTagName( tag ); 31 | for( var i = 0; i < codes.length; ++i ) 32 | { 33 | sgsCC( codes[ i ] ); 34 | } 35 | } 36 | 37 | window.onload = function() 38 | { 39 | sgsCCify( 'CODE' ); 40 | sgsCCify( 'PRE' ); 41 | var ems = document.getElementsByTagName( "EM" ); 42 | for( var i = 0; i < ems.length; ++i ) 43 | { 44 | var icon = document.createElement( 'SPAN' ); 45 | icon.setAttribute( "class", "notice" ); 46 | icon.appendChild( document.createTextNode( "i" ) ); 47 | ems[ i ].insertBefore( icon, ems[ i ].firstChild ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /docs/error_policy.txt: -------------------------------------------------------------------------------- 1 | 2 | - E: error 3 | - W: warning 4 | 5 | main policy guidelines: 6 | - anything that prevents the script from executing code is an Error 7 | - anything that has more chance of being unexpected than not is an Error 8 | -- for example, when given types/values don't support the intended operation 9 | -- not having properties may be expected so it's a warning - breaks while debugging, no issues otherwise 10 | - the main exception is cascading failures 11 | -- should disable code paths not stop things while still being detectable 12 | -- null variables are key to this - most parsing functions don't accept them, they're returned on errors 13 | 14 | 15 | CONTEXT 16 | E Failed to read bytecode file (incomplete file|invalid variable type found) 17 | 18 | TOKENIZER 19 | E End of string not found 20 | E Invalid operator found 21 | E Failed to parse numeric constant 22 | E Unexpected symbol 23 | E Comment has no end 24 | 25 | FUNCTION TREE GENERATOR 26 | E Unexpected end of code 27 | E Unexpected token while parsing argument .. 28 | E Expected ',' or '' 29 | E Invalid number of arguments in an array accessor 30 | E Missing operators or separators 31 | E Invalid expression 32 | E Unexpected token '' found 33 | E Argument name cannot be a reserved keyword 34 | E Expected initializing expression 35 | E Expected key identifier in dictionary expression 36 | E Expected '=' in dictionary expression / missing closing bracket before '{' 37 | E INTERNAL ERROR in parse_exp: unknown token found! 38 | E Empty expression found 39 | E Expected '(' after '(if|while|for|foreach|function)' 40 | E Expected 'while' after statement in do/while 41 | E Expected identifier after '(' in 'foreach' 42 | E Expected ':' or ',' after identifier in 'foreach' 43 | E Expected identifier or ':' after ',' in 'foreach' 44 | E Expected ':' after identifier #2 or ',' in 'foreach' 45 | E Expected identifier after 'function' 46 | E Expected identifier after 'function', identifier and '.' 47 | E Expected '(' after 'function' and its name 48 | E Found 'else' without matching 'if' 49 | E Invalid (break|continue) level (can be between 1 and 255) 50 | 51 | BYTECODE GENERATOR 52 | E Variable storage redefined: global -> local 53 | E Variable storage redefined: local -> global 54 | E Variable storage redefined (foreach key variable cannot be global): global -> local 55 | E Cannot redeclare arguments with the same name 56 | E Maximum number of constants exceeded 57 | E Maximum register count exceeded 58 | E This function is not a method, cannot use 'this' 59 | E Cannot read from specified keyword 60 | E Cannot write to reserved keywords 61 | E Cannot set indexed value of a constant 62 | E Cannot set property of a constant 63 | E Cannot write to constants 64 | E Too many expected outputs for operator 65 | E Empty expression found 66 | E Break level too high 67 | E Attempted to break while not in a loop 68 | E Continue level too high 69 | E Attempted to continue while not in a loop 70 | E Expression writes only allowed with function call reads 71 | E 'if' pseudo-function cannot be used as input for expression writes 72 | E 'if' pseudo-function requires exactly 3 arguments 73 | E INTERNAL ERROR: constant doesn't have a token of type int/real/string attached 74 | E Unexpected tree node [uncaught/internal (BcG/w|BcG/r|BcG/r[fe]|BcG) error] 75 | 76 | VIRTUAL MACHINE 77 | E Max call stack size reached 78 | E Object does not support negation 79 | E Negating variable of type .. isn't supported 80 | E Cannot (in|de)crement non-numeric variables 81 | E Division by 0 82 | E Specified arithmetic operation is not supported on the given set of arguments 83 | E Variable of type .. doesn't have an iterator 84 | E Object .. doesn't have an iterator 85 | E Failed to retrieve data from iterator 86 | E Object could not be called 87 | E Variable of type .. cannot be called 88 | E Illegal instruction executed 89 | E Unknown memory error // (usually means that a wrong type ID is found) 90 | W Thiscall was not called on a function type 91 | W Thiscall expects at least one argument 92 | W Thiscall failed with error .. 93 | W Expected integer as string index 94 | W String index out of bounds 95 | W Cannot index variable of type .. 96 | W Property .. not found on object of type .. 97 | W Cannot find value by index / Property not found 98 | W Index out of bounds 99 | W Invalid value used for (index|property) (read|write) 100 | W Unknown error on (index|property) (read|write) 101 | W Cannot serialize functions 102 | W Variable .. was not found 103 | 104 | STANDARD LIBRARY 105 | W ~ Most issues with arguments / state - if not specified otherwise, warning is used 106 | E This errno value is unsupported 107 | W Array index out of bounds 108 | E _G only accepts 'dict' values 109 | E unknown error while dumping variable 110 | E failed to concatenate the output 111 | E failed to serialize 112 | E unserialize: (error in data|could not find something|unknown error) 113 | -------------------------------------------------------------------------------- /docs/sgs.json.docs.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | SGScript JSON library 5 | 6 | 7 | 8 |

SGScript JSON library

Table of Contents

11 |

Description

12 | The goal of this library is to provide a simple method to encode/decode a limited-size JSON string/data set. More info about the JSON format can be found here: json.org 13 |

14 | The library is compiled to a 'sgsjson' shared library so it can be included this way (assuming that, on Linux and similar systems, LD_LIBRARY_PATH is set correctly): 15 |

include "sgsjson";
16 |

SGScript API

Functions:

  • json_encode - encode SGScript variable structure as JSON string 17 |
  • json_decode - decode JSON string to SGScript variable structure
18 |

json_encode [function]

json_encode( var )

encode SGScript variable structure as JSON string

json_encode( { a = 5, text = "something" } ) // returns {"a":5,"text":"something"}
19 |

json_decode [function]

json_decode( string jsondata )

decode JSON string to SGScript variable structure

json_decode( '{"a":5,"text":"something"}' ) // returns { a = 5, text = "something" }
20 |
21 | 22 | -------------------------------------------------------------------------------- /docs/sgs.json.docs.txt: -------------------------------------------------------------------------------- 1 | SGScript JSON library 2 | 3 | 4 | # Description [info] 5 | 6 | The goal of this library is to provide a simple method to encode/decode a limited-size JSON string/data set. More info about the JSON format can be found here: @"json.org" 7 | 8 | The library is compiled to a 'sgsjson' shared library so it can be included this way (assuming that, on Linux and similar systems, LD_LIBRARY_PATH is set correctly): 9 | 10 | include "sgsjson"; 11 | 12 | 13 | # SGScript API [info] 14 | 15 | === Functions: 16 | 17 | - @json_encode - encode SGScript variable structure as JSON string 18 | - @json_decode - decode JSON string to SGScript variable structure 19 | 20 | 21 | # >>> 22 | 23 | 24 | # json_encode [function] 25 | 26 | == json_encode( var ) 27 | === encode SGScript variable structure as JSON string 28 | 29 | json_encode( { a = 5, text = "something" } ) // returns {"a":5,"text":"something"} 30 | 31 | 32 | # json_decode [function] 33 | 34 | == json_decode( string jsondata ) 35 | === decode JSON string to SGScript variable structure 36 | 37 | json_decode( '{"a":5,"text":"something"}' ) // returns { a = 5, text = "something" } 38 | 39 | 40 | # <<< 41 | 42 | 43 | -------------------------------------------------------------------------------- /dotnet/APITest/APITest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E} 9 | Exe 10 | Properties 11 | APITest 12 | APITest 13 | v2.0 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | false 28 | 29 | 30 | x86 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | 38 | 39 | true 40 | bin\x64\Debug\ 41 | DEBUG;TRACE 42 | full 43 | x64 44 | false 45 | prompt 46 | MinimumRecommendedRules.ruleset 47 | 48 | 49 | bin\x64\Release\ 50 | TRACE 51 | true 52 | pdbonly 53 | x64 54 | prompt 55 | MinimumRecommendedRules.ruleset 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | {CB0E7CEF-1091-4433-9993-2C196B7C2765} 67 | SGS.NET 68 | 69 | 70 | 71 | 72 | 73 | 74 | 81 | -------------------------------------------------------------------------------- /dotnet/APITest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("APITest")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("SGScript")] 12 | [assembly: AssemblyProduct("APITest")] 13 | [assembly: AssemblyCopyright("Copyright © Arvīds Kokins 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c954092c-0912-42c8-8858-4db9f7524835")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /dotnet/APITest/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /dotnet/SGS.NET.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual C# Express 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SGS.NET", "SGS.NET\SGS.NET.csproj", "{CB0E7CEF-1091-4433-9993-2C196B7C2765}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VM", "VM\VM.csproj", "{8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APITest", "APITest\APITest.csproj", "{ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Debug|Mixed Platforms = Debug|Mixed Platforms 14 | Debug|x64 = Debug|x64 15 | Debug|x86 = Debug|x86 16 | Release|Any CPU = Release|Any CPU 17 | Release|Mixed Platforms = Release|Mixed Platforms 18 | Release|x64 = Release|x64 19 | Release|x86 = Release|x86 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU 25 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU 26 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|x64.ActiveCfg = Debug|Any CPU 27 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|x64.Build.0 = Debug|Any CPU 28 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Debug|x86.ActiveCfg = Debug|Any CPU 29 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU 32 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|Mixed Platforms.Build.0 = Release|Any CPU 33 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|x64.ActiveCfg = Release|Any CPU 34 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|x64.Build.0 = Release|Any CPU 35 | {CB0E7CEF-1091-4433-9993-2C196B7C2765}.Release|x86.ActiveCfg = Release|Any CPU 36 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|Any CPU.ActiveCfg = Debug|x86 37 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 38 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|Mixed Platforms.Build.0 = Debug|x86 39 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|x64.ActiveCfg = Debug|x64 40 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|x64.Build.0 = Debug|x64 41 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|x86.ActiveCfg = Debug|x86 42 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Debug|x86.Build.0 = Debug|x86 43 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|Any CPU.ActiveCfg = Release|x86 44 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|Mixed Platforms.ActiveCfg = Release|x86 45 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|Mixed Platforms.Build.0 = Release|x86 46 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|x64.ActiveCfg = Release|x64 47 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|x64.Build.0 = Release|x64 48 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|x86.ActiveCfg = Release|x86 49 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD}.Release|x86.Build.0 = Release|x86 50 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|Any CPU.ActiveCfg = Debug|x86 51 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 52 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|Mixed Platforms.Build.0 = Debug|x86 53 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|x64.ActiveCfg = Debug|x64 54 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|x64.Build.0 = Debug|x64 55 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|x86.ActiveCfg = Debug|x86 56 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Debug|x86.Build.0 = Debug|x86 57 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|Any CPU.ActiveCfg = Release|x86 58 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|Mixed Platforms.ActiveCfg = Release|x86 59 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|Mixed Platforms.Build.0 = Release|x86 60 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|x64.ActiveCfg = Release|x64 61 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|x64.Build.0 = Release|x64 62 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|x86.ActiveCfg = Release|x86 63 | {ABEE3A42-7E53-408C-8BFE-97075CF5CE1E}.Release|x86.Build.0 = Release|x86 64 | EndGlobalSection 65 | GlobalSection(SolutionProperties) = preSolution 66 | HideSolutionNode = FALSE 67 | EndGlobalSection 68 | EndGlobal 69 | -------------------------------------------------------------------------------- /dotnet/SGS.NET/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SGS.NET")] 9 | [assembly: AssemblyDescription("SGScript.NET")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("SGScript")] 12 | [assembly: AssemblyProduct("SGS.NET")] 13 | [assembly: AssemblyCopyright("Copyright © Arvīds Kokins 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("47209565-48c2-4b80-bcb1-816a169e7491")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /dotnet/SGS.NET/SGS.NET.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {CB0E7CEF-1091-4433-9993-2C196B7C2765} 9 | Library 10 | Properties 11 | SGS.NET 12 | SGS.NET 13 | v2.0 14 | 512 15 | 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | true 26 | false 27 | 28 | 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | true 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /dotnet/VM/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SGSVM.NET")] 9 | [assembly: AssemblyDescription("SGScript.NET VM")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("SGScript")] 12 | [assembly: AssemblyProduct("SGSVM.NET")] 13 | [assembly: AssemblyCopyright("Copyright © Arvīds Kokins 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("213eb6ca-cf67-4997-b8f7-9fc0b407ff17")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /dotnet/VM/VM.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using SGScript; 3 | 4 | class VM 5 | { 6 | static bool printVersion = false; 7 | static bool printStats = false; 8 | static bool separate = false; 9 | static Engine engine; 10 | 11 | static void PrintHelp() 12 | { 13 | Console.WriteLine( "syntax:" ); 14 | Console.WriteLine( "\tsgsvm [files|options]" ); 15 | Console.WriteLine( "\tsgsvm [options] -p [, ]" ); 16 | Console.WriteLine( "" ); 17 | Console.WriteLine( "options:" ); 18 | Console.WriteLine( "\t-h, --help: print this text" ); 19 | Console.WriteLine( "\t-v, --version: print version info" ); 20 | Console.WriteLine( "\t-s, --separate: restart the engine between scripts" ); 21 | // Console.WriteLine( "\t-d, --debug: enable interactive debugging on errors" ); 22 | Console.WriteLine( "\t-p, --program: translate the following arguments into a SGS program call" ); 23 | Console.WriteLine( "\t--stats: print VM stats after running the scripts" ); 24 | // Console.WriteLine( "\t--profile: enable profiling by collecting call stack timings" ); 25 | // Console.WriteLine( "\t--profile-ops: enable low-level VM instruction profiling" ); 26 | // Console.WriteLine( "\t--profile-mem: enable memory usage profiling" ); 27 | } 28 | 29 | static void PrintVersion() 30 | { 31 | if( printVersion ) 32 | Console.WriteLine( "SGSVM.NET [SGScript v{0}]", NI.Version ); 33 | } 34 | 35 | static void Init() 36 | { 37 | engine = new Engine(); 38 | } 39 | static void Free() 40 | { 41 | if( printStats ) 42 | engine.Stat( Stat.DumpStats ); 43 | engine.Release(); 44 | } 45 | 46 | static void PrintErr( string err ) 47 | { 48 | Console.Error.WriteLine( "SGSVM.NET Error: " + err ); 49 | } 50 | 51 | static int Main(string[] args) 52 | { 53 | if( args.Length < 1 ) 54 | { 55 | PrintErr( "need to specify at least one file" ); 56 | PrintHelp(); 57 | return 1; 58 | } 59 | 60 | for( int i = 0; i < args.Length; ++i ) 61 | { 62 | if( args[ i ] == "--separate" || args[ i ] == "-s" ){ separate = true; args[ i ] = null; } 63 | // else if( args[ i ] == "--debug" || args[ i ] == "-d" ){ idbg = 1; args[ i ] = null; } 64 | // else if( args[ i ] == "--profile" ){ prof = 1; args[ i ] = null; } 65 | // else if( args[ i ] == "--profile-ops" ){ prof = 2; args[ i ] = null; } 66 | // else if( args[ i ] == "--profile-mem" ){ prof = 3; args[ i ] = null; } 67 | else if( args[ i ] == "--help" || args[ i ] == "-h" ){ PrintHelp(); return 0; } 68 | else if( args[ i ] == "--version" || args[ i ] == "-v" ){ printVersion = true; args[ i ] = null; } 69 | else if( args[ i ] == "--stats" ){ printStats = true; args[ i ] = null; } 70 | else if( args[ i ] == "--program" || args[ i ] == "-p" ) 71 | { 72 | i++; 73 | if( i == args.Length ) 74 | { 75 | PrintErr( "file name expected" ); 76 | return 1; 77 | } 78 | 79 | PrintVersion(); 80 | Init(); 81 | 82 | for( int j = i; j < args.Length; ++j ) 83 | engine.Push( args[ j ] ); 84 | engine.PushArray( args.Length - i ); 85 | engine.SetGlobal( "argv", engine.StackItem( -1 ) ); 86 | engine.Pop( 1 ); 87 | 88 | engine.SetGlobal( "argc", engine.Var( args.Length - i ) ); 89 | 90 | try 91 | { 92 | engine.Include( args[ i ] ); 93 | } 94 | catch( SGSException ex ) 95 | { 96 | PrintErr( string.Format( "failed to run \"{0}\": {1}", args[ i ], ex.Message ) ); 97 | } 98 | Free(); 99 | return 0; 100 | } 101 | } 102 | 103 | PrintVersion(); 104 | Init(); 105 | for( int i = 0; i < args.Length; ++i ) 106 | { 107 | if( args[ i ] != null ) 108 | { 109 | try 110 | { 111 | engine.ExecFile( args[ i ] ); 112 | } 113 | catch( SGSException ex ) 114 | { 115 | PrintErr( string.Format( "failed to execute \"{0}\": {1}", args[ i ], ex.Message ) ); 116 | } 117 | if( separate ) 118 | { 119 | Free(); 120 | Init(); 121 | } 122 | } 123 | } 124 | 125 | Free(); 126 | return 0; 127 | } 128 | } 129 | 130 | -------------------------------------------------------------------------------- /dotnet/VM/VM.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {8C4EB7ED-8B2E-4285-89AC-D447B7663FBD} 9 | Exe 10 | Properties 11 | VM 12 | sgsvmn 13 | v2.0 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | false 28 | 29 | 30 | x86 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | 38 | 39 | true 40 | bin\x64\Debug\ 41 | DEBUG;TRACE 42 | full 43 | x64 44 | false 45 | prompt 46 | MinimumRecommendedRules.ruleset 47 | 48 | 49 | bin\x64\Release\ 50 | TRACE 51 | true 52 | pdbonly 53 | x64 54 | prompt 55 | MinimumRecommendedRules.ruleset 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | {CB0E7CEF-1091-4433-9993-2C196B7C2765} 67 | SGS.NET 68 | 69 | 70 | 71 | 72 | 73 | 74 | 81 | -------------------------------------------------------------------------------- /dotnet/VM/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /examples/AndroidSampleProject/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /examples/AndroidSampleProject/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /examples/AndroidSampleProject/jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | LOCAL_MODULE := sgs_sample 5 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../src $(LOCAL_PATH)/../../../ext 6 | LOCAL_SRC_FILES := interface.c 7 | LOCAL_SHARED_LIBRARIES := sgscript sgsxgmath 8 | include $(BUILD_SHARED_LIBRARY) 9 | 10 | include $(LOCAL_PATH)/../../../jni/Android.mk 11 | -------------------------------------------------------------------------------- /examples/AndroidSampleProject/jni/interface.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | JNIEXPORT jstring JNICALL Java_com_sgscript_sample_Main_initAndDumpGlobals( JNIEnv* env, jobject self, jstring packageName ) 8 | { 9 | SGS_CTX = sgs_CreateEngine(); 10 | 11 | // set package name 12 | const char* nPackageName = (*env)->GetStringUTFChars( env, packageName, 0 ); 13 | sgs_PushString( C, nPackageName ); 14 | sgs_SetGlobalByName( C, "ANDROID_PACKAGE_NAME", sgs_StackItem( C, -1 ) ); 15 | sgs_Pop( C, 1 ); 16 | (*env)->ReleaseStringUTFChars( env, packageName, nPackageName ); 17 | 18 | // give access to native modules 19 | sgs_ExecString( C, 20 | "foreach( p : multiply_path_ext_lists( '/data/data/' .. ANDROID_PACKAGE_NAME .. '/lib' ) )" 21 | " _G.SGS_PATH ..= ';' .. p;" ); 22 | 23 | // load a native module through the plugin system 24 | sgs_Include( C, "sgsxgmath" ); 25 | 26 | // a simple library symbol sharing test 27 | // - parser called from one SO instance should recognize object created from another instance 28 | sgs_AdjustStack( C, 1, sgs_EvalString( C, "return vec3(1,2,3);" ) ); 29 | float v[3]; 30 | sgs_SetGlobalByName( C, "SHARED_SYMBOLS", sgs_MakeBool( sgs_ParseVec3( C, -1, v, 0 ) ) ); 31 | 32 | // dump environment to string 33 | sgs_PushEnv( C ); 34 | sgs_GlobalCall( C, "dumpvar", 1, 1 ); 35 | jstring out = (*env)->NewStringUTF( env, sgs_ToString( C, -1 ) ); 36 | 37 | // clean up 38 | sgs_DestroyEngine( C ); 39 | return out; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /examples/AndroidSampleProject/src/com/sgscript/sample/Main.java: -------------------------------------------------------------------------------- 1 | package com.sgscript.sample; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | import android.widget.LinearLayout; 7 | import android.text.method.ScrollingMovementMethod; 8 | 9 | public class Main extends Activity 10 | { 11 | @Override 12 | public void onCreate(Bundle savedInstanceState) 13 | { 14 | super.onCreate(savedInstanceState); 15 | TextView label = new TextView(this); 16 | label.setText(initAndDumpGlobals(getPackageName())); 17 | label.setLayoutParams(new LinearLayout.LayoutParams( 18 | LinearLayout.LayoutParams.MATCH_PARENT, 19 | LinearLayout.LayoutParams.MATCH_PARENT)); 20 | label.setMovementMethod(new ScrollingMovementMethod()); 21 | setContentView(label); 22 | } 23 | 24 | public static native String initAndDumpGlobals( String packageName ); 25 | static 26 | { 27 | // all libraries used statically should be referenced here 28 | // plugins can be loaded by adding additional paths 29 | System.loadLibrary("sgscript"); 30 | System.loadLibrary("sgsxgmath"); 31 | System.loadLibrary("sgs_sample"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /examples/action-flow.sgs: -------------------------------------------------------------------------------- 1 | 2 | 3 | function pseudosleep( time ) 4 | { 5 | t = ftime(); 6 | while( ftime() - t < time ); 7 | } 8 | 9 | 10 | global Action = {}; 11 | 12 | function Action.create( init ) 13 | { 14 | data = 15 | { 16 | name = '', 17 | type = '', 18 | onstart = null, 19 | ontick = null, 20 | onend = null, 21 | ended = false, 22 | }; 23 | foreach( k, v : init ) 24 | data[ k ] = v; 25 | return class( data, Action ); 26 | } 27 | 28 | function Action.createTimed( init ) 29 | { 30 | if( !isset( init, "time" ) ) 31 | init.time = 1.0; 32 | if( !isset( init, "type" ) ) 33 | init.type = "Timed"; 34 | 35 | if( isset( init, "ontick" ) ) 36 | { 37 | oldtick = init.ontick; 38 | init.ontick = function() use( oldtick ) 39 | { 40 | this.time -= DT; 41 | if( this.time <= 0 ) 42 | this.end(); 43 | else if( oldtick ) 44 | oldtick.call( this ); 45 | }; 46 | } 47 | else 48 | { 49 | init.ontick = function() 50 | { 51 | this.time -= DT; 52 | if( this.time <= 0 ) 53 | this.end(); 54 | }; 55 | } 56 | 57 | return Action.create( init ); 58 | } 59 | 60 | function Action.end() 61 | { 62 | this.ended = true; 63 | } 64 | 65 | global ActionFlow = 66 | { 67 | actions = [], 68 | }; 69 | 70 | function ActionFlow.tick() 71 | { 72 | var AA = this.actions; 73 | for( i = 0; i < AA.size; ++i ) 74 | { 75 | action = AA[ i ]; 76 | if( action.ontick ) 77 | action.ontick(); 78 | if( action.ended ) 79 | { 80 | if( action.onend ) 81 | action.onend(); 82 | AA.erase( i-- ); 83 | } 84 | } 85 | } 86 | 87 | function ActionFlow.addAction( act ) 88 | { 89 | this.actions.push( act ); 90 | if( act.onstart ) 91 | act.onstart(); 92 | } 93 | 94 | function ActionFlow.hasActions() 95 | { 96 | return !!this.actions; 97 | } 98 | 99 | 100 | // Test prep 101 | 102 | function intro_fade() 103 | { 104 | println( "time: ", this.time ); 105 | } 106 | function intro_end() 107 | { 108 | ActionFlow.addAction( Action.createTimed({ named = "Stopping...", time = 1.0 }) ); 109 | println( "Stopping in 1 sec!" ); 110 | } 111 | 112 | ActionFlow.addAction( Action.createTimed({ name = "Intro", time = 2.0, ontick = intro_fade, onend = intro_end }) ); 113 | 114 | 115 | // Test action 116 | 117 | starttime = ftime(); 118 | while( ActionFlow.hasActions() ) 119 | { 120 | curtime = ftime(); 121 | global DT = curtime - starttime; 122 | ActionFlow.tick(); 123 | starttime = curtime; 124 | 125 | pseudosleep( 0.01 ); 126 | } 127 | -------------------------------------------------------------------------------- /examples/benchmarksgame/binarytrees.sgs: -------------------------------------------------------------------------------- 1 | // The Computer Language Benchmarks Game 2 | // http://benchmarksgame.alioth.debian.org/ 3 | 4 | include "string", "math"; 5 | 6 | function BottomUpTree( item, depth ) 7 | { 8 | if( depth > 0 ) 9 | { 10 | i = item + item; 11 | depth = depth - 1; 12 | left = BottomUpTree(i-1, depth); 13 | right = BottomUpTree(i, depth); 14 | return [item, left, right]; 15 | } 16 | else 17 | { 18 | return [item]; 19 | } 20 | } 21 | 22 | function ItemCheck(tree) 23 | { 24 | if( @tree[1] ) 25 | return tree[0] + ItemCheck(tree[1]) - ItemCheck(tree[2]); 26 | else 27 | return tree[0]; 28 | } 29 | 30 | var N = toreal(@argv[1]); 31 | var mindepth = 4; 32 | var maxdepth = mindepth + 2; 33 | if( maxdepth < N ) maxdepth = N; 34 | 35 | { 36 | var stretchdepth = maxdepth + 1; 37 | var stretchtree = BottomUpTree(0, stretchdepth); 38 | print(string_format("stretch tree of depth {1:d}\t check: {2:d}\n", 39 | stretchdepth, ItemCheck(stretchtree))); 40 | } 41 | 42 | var longlivedtree = BottomUpTree(0, maxdepth); 43 | 44 | for( depth=mindepth; depth <= maxdepth; depth += 2 ) 45 | { 46 | var iterations = pow( 2, maxdepth - depth + mindepth); 47 | var check = 0; 48 | for( i=1; i <= iterations; ++i) 49 | { 50 | check = check + ItemCheck(BottomUpTree(1, depth)) + 51 | ItemCheck(BottomUpTree(-1, depth)); 52 | } 53 | print(string_format("{1:d}\t trees of depth {2:d}\t check: {3:d}\n", 54 | iterations*2, depth, check)); 55 | } 56 | 57 | print(string_format("long lived tree of depth {1:d}\t check: {2:d}\n", 58 | maxdepth, ItemCheck(longlivedtree))); 59 | -------------------------------------------------------------------------------- /examples/closure-counter.sgs: -------------------------------------------------------------------------------- 1 | // be aware that this example is written to show how closures work 2 | // and that nothing beats the good old "counter++" in terms of speed 3 | // don't write code like this unless some interface requires it 4 | 5 | i = 1; 6 | counter = function( data ) use( i ){ return i++; }; 7 | 8 | print( "first: ", counter(), "\n" ); 9 | print( "second: ", counter(), "\n" ); 10 | -------------------------------------------------------------------------------- /examples/demodumps/fapi.sgs: -------------------------------------------------------------------------------- 1 | include_library("io"); 2 | 3 | printlns 4 | ( 5 | "FILE: " $ ( f = io_file( "output.dir", FILE_READ ) ), 6 | "REOPEN: " $ f.open( "tmp", FILE_READ | FILE_WRITE ), 7 | "SETBUF: " $ f.setbuf( 123 ), 8 | "SIZE BEFORE: " $ f.size, 9 | "WRITE 'test': " $ f.write( "test" ), 10 | "FLUSH: " $ f.flush(), 11 | "SIZE AFTER: " $ f.size, 12 | "SEEK -1,end: " $ f.seek( -1, SEEK_END ), 13 | "SEEK -1,cur: " $ f.seek( -1, SEEK_CUR ), 14 | "TELL: " $ f.offset, 15 | "SEEK 1,set: " $ f.seek( 1 ), 16 | "READ: " $ f.read( 4 ), 17 | "ERROR?: " $ f.error, 18 | "EOF: " $ f.eof, 19 | "CLOSE: " $ f.close(), 20 | "CLEANUP: " $ io_file_delete( "tmp" ) 21 | ); 22 | 23 | foreach( t, i : io_dir( "." ) ) 24 | printvar( t, i ); 25 | 26 | foreach( t, i : io_dir( "nonexistent" ) ) 27 | printvar( t, i ); 28 | -------------------------------------------------------------------------------- /examples/demodumps/fmt.sgs: -------------------------------------------------------------------------------- 1 | 2 | include_library( "string" ); 3 | include_library( "fmt" ); 4 | 5 | function hexit( data ) 6 | { 7 | tbl = "0123456789ABCDEF"; 8 | out = ""; 9 | for( i = 0; i < data.length; ++i ) 10 | { 11 | if( out.length ) 12 | out $= " "; 13 | cc = data[ i ]; 14 | out $= chr( tbl[ cc >> 4 ] ); 15 | out $= chr( tbl[ cc & 15 ] ); 16 | } 17 | return out; 18 | } 19 | 20 | printlns 21 | ( 22 | "", 23 | "3s4c : numbers,51,52,53,54 = " $ fmt_pack( "3s4c", "numbers", 51, 52, 53, 54 ), 24 | "+REV = " $ fmt_unpack( "3s4c", fmt_pack( "3s4c", "numbers", 51, 52, 53, 54 ) ), 25 | "", 26 | "w : 1024 = " $ hexit( fmt_pack( "w", 1024 ) ), 27 | "+REV = " $ fmt_unpack( "w", fmt_pack( "w", 1024 ) ), 28 | "", 29 | " fxd : 13.37, 13.37 = " $ hexit( fmt_pack( "fxd", 13.37, 13.37 ) ), 30 | "+REV = " $ fmt_unpack( "fxd", fmt_pack( "fxd", 13.37, 13.37 ) ), 31 | "", 32 | ">fxd : 13.37, 13.37 = " $ hexit( fmt_pack( ">fxd", 13.37, 13.37 ) ), 33 | "+REV = " $ fmt_unpack( ">fxd", fmt_pack( ">fxd", 13.37, 13.37 ) ), 34 | "", 35 | "b64 Man = " $ fmt_base64_encode( "Man" ), 36 | "b64 A = " $ fmt_base64_encode( "the red fo" ), 37 | "b64 B = " $ fmt_base64_encode( "the red fox" ), 38 | "b64 C = " $ fmt_base64_encode( "the red foxx" ), 39 | "b64 +R A = " $ fmt_base64_decode( fmt_base64_encode( "the red fo" ) ), 40 | "b64 +R B = " $ fmt_base64_decode( fmt_base64_encode( "the red fox" ) ), 41 | "b64 +R C = " $ fmt_base64_decode( fmt_base64_encode( "the red foxx" ) ), 42 | "", 43 | "good!" 44 | ); 45 | 46 | -------------------------------------------------------------------------------- /examples/demodumps/misapi.sgs: -------------------------------------------------------------------------------- 1 | 2 | a1 = [1,3,5]; 3 | a2 = [2,4,6]; 4 | a3 = [7,8,9]; 5 | a4 = [1,2,3,4,2,5,4]; 6 | d1 = { a = "b", c = "d" }; 7 | printvar 8 | ( 9 | get_concat( a1, a2, a3 ), 10 | get_merged( d1, a4, a1 ), 11 | get_keys( d1 ), 12 | get_values( d1 ), 13 | a1.find( 3 ), 14 | a1.find( "3" ), 15 | a1.find( "3", true ), 16 | clone(a4).remove("2",false,true) 17 | ); 18 | 19 | -------------------------------------------------------------------------------- /examples/demodumps/osapi.sgs: -------------------------------------------------------------------------------- 1 | 2 | include_library( "string" ); 3 | include_library( "os" ); 4 | 5 | formats = string_explode( "a,A,b,B,c,x,X,Z,U,W,C,d,e,F,H,I,j,m,M,p,R,S,T,u,w,y,Y,f,t,%", "," ); 6 | dateproc = {}; 7 | foreach( fmt : formats ) 8 | dateproc[ fmt ] = os_date_string( "%" $ fmt ); 9 | 10 | printlns 11 | ( 12 | "OS type = " $ os_gettype(), 13 | "OS time = " $ os_time(), 14 | "OS time +2 = " $ os_time(2), 15 | "OS time +2:30 = " $ os_time(2.5), 16 | "OS time +3 = " $ os_time(3), 17 | "", 18 | "timezone (R) = " $ os_get_timezone(), 19 | "timezone (S) = " $ os_get_timezone(true), 20 | "", 21 | "date formats >", dumpvar( dateproc ), 22 | "", 23 | "parsed time >", dumpvar( os_parse_time() ), 24 | "", 25 | "made time >", dumpvar( os_make_time( 30, 30, 12, 25, 5, 2013 ) ), 26 | "", 27 | "good!" 28 | ); 29 | -------------------------------------------------------------------------------- /examples/directory-size.sgs: -------------------------------------------------------------------------------- 1 | 2 | include_library( "io" ); 3 | 4 | 5 | function dir_size_recursive( dir ) 6 | { 7 | size = 0; 8 | foreach( t, i : io_dir( dir ) ) 9 | { 10 | if( !t ) 11 | continue; 12 | fullpath = dir $ "/" $ i; 13 | info = io_stat( fullpath ); 14 | if( info.type == FST_FILE ) 15 | size += info.size; 16 | else if( info.type == FST_DIR ) 17 | size += dir_size_recursive( fullpath ); 18 | } 19 | return size; 20 | } 21 | 22 | println 23 | ( 24 | "Directory size (recursive counter): ", 25 | dir_size_recursive( "." ) / 1024, " kB" 26 | ); 27 | 28 | 29 | function dir_size_nonrecursive( dir ) 30 | { 31 | set = [dir]; 32 | size = 0; 33 | 34 | while( set.size ) 35 | { 36 | path = set.pop(); 37 | foreach( t, i : io_dir( path ) ) 38 | { 39 | if( !t ) 40 | continue; 41 | fullpath = path $ "/" $ i; 42 | info = io_stat( fullpath ); 43 | if( info.type == FST_FILE ) 44 | size += info.size; 45 | else if( info.type == FST_DIR ) 46 | set.push( fullpath ); 47 | } 48 | } 49 | 50 | return size; 51 | } 52 | 53 | println 54 | ( 55 | "Directory size (non-recursive counter): ", 56 | dir_size_nonrecursive( "." ) / 1024, " kB" 57 | ); 58 | -------------------------------------------------------------------------------- /examples/hash-test.sgs: -------------------------------------------------------------------------------- 1 | 2 | include "string"; 3 | 4 | function string_random( chars, length ) 5 | { 6 | return string_implode( chars.random( length ), "" ); 7 | } 8 | 9 | 10 | srand( ftime() * 1000 ); 11 | STRING_CHARS = string_explode( "abcdefghjiklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ"$ 12 | "0123456789 .,/;:<>[](){}?!@#$%^&*-=_+~`\\|", "" ); 13 | STRING_COUNT = 100000; 14 | STRINGS = []; 15 | for( i = 0; i < STRING_COUNT; ++i ) 16 | STRINGS.push( string_random( STRING_CHARS, rand() % 12 + 8 ) ); 17 | 18 | 19 | function test_hashfunc( name, strings ) 20 | { 21 | func = _G[ name ]; 22 | hashes = map(); 23 | t1 = ftime(); 24 | foreach( string : strings ) 25 | hashes[ func( string ) ] = true; 26 | t2 = ftime(); 27 | cc = strings.size - map_size( hashes ); 28 | 29 | printlns( 30 | "Hash function '" $ name $ "'", 31 | "Time: " $ t2 - t1, 32 | "Collisions: " $ cc $ " / " $ strings.size $ " (" $ cc/strings.size $ ")", 33 | "" 34 | ); 35 | } 36 | 37 | 38 | test_hashfunc( "hash_fnv", STRINGS ); 39 | 40 | -------------------------------------------------------------------------------- /examples/memory-test.sgs: -------------------------------------------------------------------------------- 1 | 2 | function memusage() 3 | { 4 | println( "Memory usage: ", sys_stat( 3 ) / 1024, " kB" ); 5 | } 6 | 7 | numobjs = 16384; 8 | interval = 1024; 9 | 10 | memusage(); 11 | data = []; 12 | 13 | println( "--- LOADING ---" ); 14 | for( i = 0; i < numobjs; ++i ) 15 | { 16 | data.push({ two = "cool", four = "school" }); 17 | if( i % interval == interval - 1 ) 18 | memusage(); 19 | } 20 | 21 | println( "--- unLOADING ---" ); 22 | for( i = 0; i < numobjs; ++i ) 23 | { 24 | data.pop(); 25 | if( i % interval == interval - 1 ) 26 | memusage(); 27 | } 28 | 29 | println( "--- done! ---" ); 30 | data = null; 31 | memusage(); 32 | 33 | -------------------------------------------------------------------------------- /examples/opchain-test.sgs: -------------------------------------------------------------------------------- 1 | 2 | include_library( "string" ); 3 | include_library( "io" ); 4 | 5 | sum = 1000; 6 | code = "print(" $ string_repeat( "+1", sum - 1 ) $ ");"; 7 | eval( code ); 8 | -------------------------------------------------------------------------------- /examples/pagination.sgs: -------------------------------------------------------------------------------- 1 | /* 2 | A simple page usefulness-based pagination 3 | algorithm demonstrates the power of 4 | sort_mapped (sorting one array by another) 5 | "the power", of course, might feel like an 6 | overblown statement considering there's only 7 | one line of it but that's it - all you need 8 | is one line to make it work (as opposed to 9 | 50+ lines and a custom sorting algorithm 10 | that doesn't get near the speed of C) 11 | */ 12 | 13 | include_library( "string" ); 14 | include_library( "math" ); 15 | 16 | numpages = 137; 17 | numslots = 9; 18 | curpage = 8; 19 | 20 | pages = []; 21 | weights = []; 22 | for( i = 1; i <= numpages; ++i ) 23 | { 24 | w = 1 / i + 1 / ( numpages + 1 - i ) + 25 | 1 / ( abs( i - curpage ) + 1 ); 26 | pages.push( i ); 27 | weights.push( w ); 28 | } 29 | 30 | pages.sort_mapped( weights, true ); 31 | if( pages.size > numslots ) 32 | pages.resize( numslots ); 33 | pages.sort(); 34 | 35 | // printvar( pages ); 36 | 37 | println( "numpages = ", numpages ); 38 | println( "numslots = ", numslots ); 39 | println( "curpage = ", curpage ); 40 | 41 | out = '|'; 42 | foreach( p : pages ) 43 | { 44 | padchr = ' '; 45 | if( p == curpage ) 46 | padchr = '-'; 47 | out $= string_pad( p, 3, padchr, 48 | STRING_PAD_LEFT | STRING_PAD_RIGHT ) $ '|'; 49 | } 50 | side = string_repeat( '-', out.length ); 51 | 52 | println( side ); 53 | println( out ); 54 | println( side ); 55 | -------------------------------------------------------------------------------- /examples/recursion-test.sgs: -------------------------------------------------------------------------------- 1 | 2 | function recurse() 3 | { 4 | eval( "recurse();" ); 5 | } 6 | recurse(); 7 | -------------------------------------------------------------------------------- /examples/sandbox.sgs: -------------------------------------------------------------------------------- 1 | restrict = function( name ) 2 | { 3 | println( "==\n= The use of '" $ name $ "' is restricted!\n==" ); 4 | }; 5 | closure = function( clbl, data ) 6 | { 7 | return function() use( clbl, data ) 8 | { 9 | args = va_get_args(); 10 | args.unshift( data ); 11 | sys_apply( clbl, this, args ); 12 | }; 13 | }; 14 | safe_include_lib = function( data, libname ) 15 | { 16 | libname = tostring( libname ); 17 | if( ["string","math","re","fmt","os"].find( libname ) === null ) 18 | { 19 | println( "info: cannot include files and system access libraries here" ); 20 | return; 21 | } 22 | data( libname ); 23 | if( libname == "os" ) 24 | { 25 | unset( _G, "os_command" ); 26 | unset( _G, "os_getenv" ); 27 | unset( _G, "os_putenv" ); 28 | } 29 | }; 30 | safe_include = function( libname ) 31 | { 32 | return include_library( libname ); 33 | }; 34 | global include_library = closure( safe_include_lib, include_library ); 35 | global include = safe_include; 36 | global include_shared = closure( restrict, "include_shared" ); 37 | global import_cfunc = closure( restrict, "import_cfunc" ); 38 | global include_file = closure( restrict, "include_file" ); 39 | global eval_file = closure( restrict, "eval_file" ); 40 | 41 | include "string", "math", "re", "fmt", "os"; 42 | -------------------------------------------------------------------------------- /examples/sgstest_mt.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | multithreading test 4 | - makes sure there are no conflicts that could prevent this system from 5 | being used in multithreaded software 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | #define NUM_THREADS 4 15 | 16 | const char* testcode = 17 | "start = ftime();\n" 18 | "while(ftime()-start<5){\n" 19 | " test = dict();}\n" 20 | ; 21 | 22 | void* threadproc( void* arg ) 23 | { 24 | SGS_CTX = sgs_CreateEngine(); 25 | sgs_ExecString( C, testcode ); 26 | sgs_DestroyEngine( C ); 27 | return NULL; 28 | } 29 | 30 | int main() 31 | { 32 | int i, rc; 33 | pthread_t threads[ NUM_THREADS ]; 34 | 35 | printf( "SGScript Multithreading Test\n- runs for 5 seconds and exits\n" ); 36 | 37 | for( i = 0; i < NUM_THREADS; ++i ) 38 | { 39 | rc = pthread_create( threads + i, NULL, threadproc, NULL ); 40 | assert( !rc ); 41 | } 42 | 43 | for( i = 0; i < NUM_THREADS; ++i ) 44 | { 45 | rc = pthread_join( threads[ i ], NULL ); 46 | assert( !rc ); 47 | } 48 | 49 | printf( "\ndone!\n" ); 50 | 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /examples/speed-dict.sgs: -------------------------------------------------------------------------------- 1 | 2 | function fmt_bytenum( n ) 3 | { 4 | on = n; 5 | i = 0; 6 | units = [ "B", "kB", "MB" ]; 7 | while( n > 2048 && i < units.size - 1 ) 8 | { 9 | n /= 1024; 10 | i++; 11 | } 12 | return n $ " " $ units[i] $ " (" $ on $ " bytes)"; 13 | } 14 | 15 | count = 100000; 16 | 17 | mem_base = sys_stat(3); 18 | num_base = sys_stat(6); 19 | obj = {}; 20 | t0 = ftime(); 21 | for( var i = 0; i < count; ++i ) 22 | obj[ i ] = ftime(); 23 | mem_load = sys_stat(3); 24 | num_load = sys_stat(6); 25 | 26 | t1 = ftime(); 27 | for( var i = 0; i < count; ++i ) 28 | dummy = obj[ i ]; 29 | 30 | t2 = ftime(); 31 | for( var i = 0; i < count; ++i ) 32 | unset( obj, i ); 33 | mem_unload = sys_stat(3); 34 | num_unload = sys_stat(6); 35 | 36 | t3 = ftime(); 37 | 38 | obj = null; 39 | mem_free = sys_stat(3); 40 | 41 | printlns( "-- info --", "count: " $ count ); 42 | printlns( "-- time --", "set: " $ t1-t0, "get: " $ t2-t1, "unset: " $ t3-t2 ); 43 | printlns 44 | ( 45 | "-- memory --", 46 | "before: " $ fmt_bytenum( mem_base ), 47 | "load: " $ fmt_bytenum( mem_load ), 48 | "unload: " $ fmt_bytenum( mem_unload ), 49 | "memory after dict free: " $ fmt_bytenum( mem_free ), 50 | "delta: " $ fmt_bytenum( mem_load - mem_base ), 51 | "junk memory after unset: " $ fmt_bytenum( mem_unload - mem_free ), 52 | "accumulated overhead: " $ fmt_bytenum( mem_free - mem_base ), 53 | "-- allocation count --", 54 | "before: " $ num_base, 55 | "load: " $ num_load, 56 | "unload: " $ num_unload 57 | ); 58 | -------------------------------------------------------------------------------- /examples/speed-map.sgs: -------------------------------------------------------------------------------- 1 | 2 | function fmt_bytenum( n ) 3 | { 4 | on = n; 5 | i = 0; 6 | units = [ "B", "kB", "MB" ]; 7 | while( n > 2048 && i < units.size - 1 ) 8 | { 9 | n /= 1024; 10 | i++; 11 | } 12 | return n $ " " $ units[i] $ " (" $ on $ " bytes)"; 13 | } 14 | 15 | count = 100000; 16 | 17 | mem_base = sys_stat(3); 18 | num_base = sys_stat(6); 19 | obj = map(); 20 | t0 = ftime(); 21 | for( var i = 0; i < count; ++i ) 22 | obj[ i ] = ftime(); 23 | mem_load = sys_stat(3); 24 | num_load = sys_stat(6); 25 | 26 | t1 = ftime(); 27 | for( var i = 0; i < count; ++i ) 28 | dummy = obj[ i ]; 29 | 30 | t2 = ftime(); 31 | for( var i = 0; i < count; ++i ) 32 | unset( obj, i ); 33 | mem_unload = sys_stat(3); 34 | num_unload = sys_stat(6); 35 | 36 | t3 = ftime(); 37 | 38 | obj = null; 39 | mem_free = sys_stat(3); 40 | 41 | printlns( "-- info --", "count: " $ count ); 42 | printlns( "-- time --", "set: " $ t1-t0, "get: " $ t2-t1, "unset: " $ t3-t2 ); 43 | printlns 44 | ( 45 | "-- memory --", 46 | "before: " $ fmt_bytenum( mem_base ), 47 | "load: " $ fmt_bytenum( mem_load ), 48 | "unload: " $ fmt_bytenum( mem_unload ), 49 | "memory after dict free: " $ fmt_bytenum( mem_free ), 50 | "delta: " $ fmt_bytenum( mem_load - mem_base ), 51 | "junk memory after unset: " $ fmt_bytenum( mem_unload - mem_free ), 52 | "accumulated overhead: " $ fmt_bytenum( mem_free - mem_base ), 53 | "-- allocation count --", 54 | "before: " $ num_base, 55 | "load: " $ num_load, 56 | "unload: " $ num_unload 57 | ); 58 | -------------------------------------------------------------------------------- /examples/threaded-load.sgs: -------------------------------------------------------------------------------- 1 | 2 | include_library( "fmt" ); 3 | include_library( "string" ); 4 | include_shared( "sgspproc" ); 5 | 6 | 7 | global PPROC = pproc_create(); 8 | 9 | function _stream_threadproc() 10 | { 11 | include_library( "io" ); 12 | 13 | function lock(){ while( !_T.set_if( "lock", true, false ) ); } 14 | function unlock(){ _T.set( "lock", false ); } 15 | 16 | while( !_T.get( "stop" ) ) 17 | { 18 | file = null; 19 | 20 | lock(); 21 | queue = _T.get( "queue" ); 22 | if( queue.size ) 23 | { 24 | file = queue.pop(); 25 | _T.set( "queue", queue ); 26 | } 27 | unlock(); 28 | 29 | if( file !== null ) 30 | { 31 | _T.set( "file:" $ file, io_file_read( file ) ); 32 | } 33 | 34 | sleep( 100 ); 35 | } 36 | } 37 | 38 | function stream_init() 39 | { 40 | global FS = PPROC.add_job( _stream_threadproc ); 41 | 42 | FS.set( "lock", false ); 43 | FS.set( "stop", false ); 44 | FS.set( "queue", [] ); 45 | 46 | global FS_lock = function(){ while( !FS.set_if( "lock", true, false ) ); }; 47 | global FS_unlock = function(){ FS.set( "lock", false ); }; 48 | 49 | FS.start(); 50 | } 51 | 52 | function stream_close() 53 | { 54 | FS.set( "stop", true ); 55 | FS.wait(); 56 | } 57 | 58 | function stream_file( file ) 59 | { 60 | if( stream_loaded( file ) ) 61 | return; 62 | FS_lock(); 63 | queue = FS.get( "queue" ); 64 | if( queue.find( file ) === null ) 65 | { 66 | queue.unshift( file ); 67 | FS.set( "queue", queue ); 68 | } 69 | FS_unlock(); 70 | } 71 | 72 | function stream_loaded( file ) 73 | { 74 | return FS.has( "file:" $ file ); 75 | } 76 | 77 | 78 | stream_init(); 79 | 80 | print( "=====\n> " ); 81 | while( ( ff = string_trim(read_stdin()) ) != '' ) 82 | { 83 | print( "! streaming: " $ ff $ " " ); 84 | stream_file( ff ); 85 | while( !stream_loaded( ff ) ) 86 | { 87 | print( "." ); 88 | sleep( 1 ); 89 | } 90 | println( "done!" ); 91 | print( "=====\n> " ); 92 | } 93 | 94 | stream_close(); 95 | -------------------------------------------------------------------------------- /ext/cppbc.cmake: -------------------------------------------------------------------------------- 1 | 2 | set( SGSVM_PATH "sgsvm" CACHE FILEPATH 3 | "Path to SGSVM (SGScript virtual machine)" ) 4 | set( SGSCPPBC_PATH "../ext/cppbc.sgs" CACHE FILEPATH 5 | "Path to cppbc.sgs (SGS/CPP-BC binding code generator)" ) 6 | 7 | if( MSVC ) 8 | set( CPPBC_INC_PFX /I ) 9 | set( CPPBC_PP_ARGS /P /DSGS_CPPBC_PP=1 /Fi ) 10 | else() 11 | set( CPPBC_INC_PFX -I ) 12 | set( CPPBC_PP_ARGS -E -DSGS_CPPBC_PP=1 -o ) 13 | endif() 14 | 15 | macro( cppbc_header TARGET CPPFILE HFILE INCNAME ) 16 | get_target_property( LIST ${TARGET} INCLUDE_DIRECTORIES ) 17 | if( NOT LIST ) 18 | set( LIST "" ) 19 | endif() 20 | separate_arguments( LIST ) 21 | set( CFLAGS "" ) 22 | foreach( F ${LIST} ) 23 | list( APPEND CFLAGS "${CPPBC_INC_PFX}${F}" ) 24 | endforeach( F ) 25 | 26 | get_target_property( LIST ${TARGET} COMPILE_FLAGS ) 27 | if( LIST ) 28 | string( REPLACE " " ";" LIST ${LIST} ) 29 | foreach( F ${LIST} ) 30 | if( ${F} MATCHES "^[/-][DI]" ) 31 | list( APPEND CFLAGS ${F} ) 32 | endif() 33 | endforeach() 34 | endif() 35 | 36 | get_target_property( LIST ${TARGET} COMPILE_DEFINITIONS ) 37 | if( LIST ) 38 | foreach( F ${LIST} ) 39 | list( APPEND CFLAGS "-D${F}" ) 40 | endforeach() 41 | endif() 42 | 43 | set( TMPFN cppbc_tmp_${HFILE} ) 44 | string( REPLACE "/" "$" TMPFN "${TMPFN}" ) 45 | string( REPLACE "\\" "$" TMPFN "${TMPFN}" ) 46 | add_custom_command( 47 | OUTPUT "${CMAKE_SOURCE_DIR}/${CPPFILE}" 48 | DEPENDS "${CMAKE_SOURCE_DIR}/${HFILE}" 49 | COMMAND ${SGSVM_PATH} ARGS -p ${SGSCPPBC_PATH} -cmake0 ${TMPFN}.cpp "${CMAKE_SOURCE_DIR}/${HFILE}" 50 | COMMAND ${CMAKE_CXX_COMPILER} ARGS ${CPPBC_PP_ARGS} ${TMPFN}.i ${TMPFN}.cpp ${CFLAGS} 51 | COMMAND ${SGSVM_PATH} ARGS -p ${SGSCPPBC_PATH} -o "${CMAKE_SOURCE_DIR}/${CPPFILE}" -iname ${INCNAME} ${TMPFN}.i 52 | ) 53 | endmacro( cppbc_header ) 54 | 55 | macro( cppbc_headers TARGET ) 56 | foreach( FILE ${ARGN} ) 57 | get_filename_component( FILE ${FILE} ABSOLUTE ) 58 | file( RELATIVE_PATH FILE ${CMAKE_SOURCE_DIR} ${FILE} ) 59 | get_filename_component( FDIR ${FILE} PATH ) 60 | get_filename_component( FNAME ${FILE} NAME_WE ) 61 | get_filename_component( FXNAME ${FILE} NAME ) 62 | set( OUTFILE "${FDIR}/cppbc_${FNAME}.cpp" ) 63 | cppbc_header( ${TARGET} ${OUTFILE} ${FILE} ${FXNAME} ) 64 | endforeach( FILE ) 65 | endmacro( cppbc_headers ) 66 | -------------------------------------------------------------------------------- /ext/sgs_dbgserver.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SGS_DBGSERVER_H_INCLUDED 3 | #define SGS_DBGSERVER_H_INCLUDED 4 | 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | 11 | #ifndef HEADER_SGSCRIPT_H 12 | # define HEADER_SGSCRIPT_H 13 | #endif 14 | #include HEADER_SGSCRIPT_H 15 | #ifndef HEADER_SGS_UTIL_H 16 | # define HEADER_SGS_UTIL_H 17 | #endif 18 | #include HEADER_SGS_UTIL_H 19 | 20 | typedef struct sgs_DebugServer sgs_DebugServer; 21 | 22 | #define SGS_DBGSRV_PORT_DISABLE 0 /* run without network, in console mode */ 23 | #define SGS_DBGSRV_PORT_DEFAULT 7473 24 | 25 | sgs_DebugServer* sgs_CreateDebugServer( SGS_CTX, int port ); 26 | void sgs_CloseDebugServer( sgs_DebugServer* D ); 27 | void sgs_DebugServerCmd( sgs_DebugServer* D, const char* cmd ); 28 | 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /ext/sgs_em.c: -------------------------------------------------------------------------------- 1 | 2 | // compile with: 3 | // emcc sgs_em.c -o sgscript.js -s EXPORTED_FUNCTIONS="['_runsgs']" -Os --memory-init-file 0 4 | 5 | #define SGS_INTERNAL_STRINGTABLES 6 | #define SGS_USE_FILESYSTEM 7 | 8 | #include "../src/sgs_util.c" 9 | #include "../src/sgs_ctx.c" 10 | #include "../src/sgs_tok.c" 11 | #include "../src/sgs_fnt.c" 12 | #include "../src/sgs_bcg.c" 13 | #include "../src/sgs_proc.c" 14 | #include "../src/sgs_regex.c" 15 | #include "../src/sgs_std.c" 16 | #include "../src/sgs_stdL.c" 17 | #include "../src/sgs_srlz.c" 18 | #include "../src/sgs_xpc.c" 19 | 20 | #define HEADER_SGSCRIPT_H "../src/sgscript.h" 21 | #include "sgsxgmath.c" 22 | 23 | #define MAX_OUTPUT_SIZE 1024*1024 24 | 25 | char* outbuf = NULL; 26 | char* outbuf_at = NULL; 27 | void output_to_buffer( void* userdata, SGS_CTX, const void* ptr, size_t size ) 28 | { 29 | char* end = outbuf + MAX_OUTPUT_SIZE; 30 | size_t realsize = end - outbuf_at - 1; 31 | if( size > realsize ) 32 | size = realsize; 33 | memcpy( outbuf_at, ptr, size ); 34 | outbuf_at += size; 35 | } 36 | 37 | char* runsgs( const char* script ) 38 | { 39 | if( !outbuf ) 40 | { 41 | outbuf = malloc( MAX_OUTPUT_SIZE ); 42 | } 43 | 44 | SGS_CTX = sgs_CreateEngine(); 45 | outbuf_at = outbuf; 46 | sgs_SetOutputFunc( C, output_to_buffer, NULL ); 47 | sgs_SetErrOutputFunc( C, output_to_buffer, NULL ); 48 | sgs_LoadLib_Fmt( C ); 49 | /* no need for I/O - can't use it from the browser */ 50 | sgs_LoadLib_Math( C ); 51 | sgs_LoadLib_OS( C ); 52 | sgs_LoadLib_RE( C ); 53 | sgs_LoadLib_String( C ); 54 | sgs_xgm_module_entry_point( C ); 55 | sgs_ExecString( C, script ); 56 | sgs_DestroyEngine( C ); 57 | *outbuf_at = 0; 58 | return outbuf; 59 | } 60 | -------------------------------------------------------------------------------- /ext/sgs_idbg.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | #include "sgs_idbg.h" 6 | 7 | 8 | /* 9 | To successfully debug recursion issues, 10 | call stack limit must be slightly raised 11 | to allow execution of debug code. 12 | */ 13 | #ifndef SGS_IDBG_STACK_EXTENSION 14 | #define SGS_IDBG_STACK_EXTENSION 128 15 | #endif 16 | 17 | 18 | #define STREQ( a, b ) ( strcmp( a, b ) == 0 ) 19 | 20 | 21 | #define BFR_SIZE 1024 22 | 23 | static void idbg_readStdin( SGS_IDBG ) 24 | { 25 | /* read from STDIN */ 26 | size_t wsz = 31, i = 0; 27 | char bfr[ BFR_SIZE ]; 28 | sgs_membuf_resize( &D->input, D->C, 0 ); 29 | while( fgets( bfr, BFR_SIZE, stdin ) ) 30 | { 31 | size_t len = strlen( bfr ); 32 | sgs_membuf_appbuf( &D->input, D->C, bfr, len ); 33 | if( len && bfr[ len - 1 ] == '\n' ) 34 | break; 35 | } 36 | sgs_membuf_appchr( &D->input, D->C, 0 ); 37 | 38 | /* parse first word */ 39 | while( i < wsz && i < D->input.size && sgs_isalpha( D->input.ptr[ i ] ) ) 40 | { 41 | D->iword[ i ] = D->input.ptr[ i ]; 42 | i++; 43 | } 44 | D->iword[ i ] = 0; 45 | } 46 | 47 | 48 | static void idbgPrintFunc( void* data, SGS_CTX, int type, const char* message ) 49 | { 50 | SGS_IDBG = (sgs_IDbg*) data; 51 | 52 | D->stkoff = C->stack_off - C->stack_base; 53 | D->stksize = C->stack_top - C->stack_base; 54 | D->pfn( D->pctx, C, type, message ); 55 | if( D->inside || type < D->minlev ) 56 | return; 57 | D->inside = 1; 58 | C->sf_count -= SGS_IDBG_STACK_EXTENSION; 59 | printf( "----- Interactive SGScript Debug Inspector -----" ); 60 | 61 | for(;;) 62 | { 63 | printf( "\n> " ); 64 | idbg_readStdin( D ); 65 | if( ferror( stdin ) ) 66 | break; 67 | if( !*D->iword ) 68 | continue; 69 | 70 | if( STREQ( D->iword, "continue" ) || STREQ( D->iword, "cont" ) ) break; 71 | if( STREQ( D->iword, "quit" ) ) exit( 0 ); 72 | if( STREQ( D->iword, "reprint" ) ) D->pfn( D->pctx, C, type, message ); 73 | if( STREQ( D->iword, "stack" ) ) sgs_Stat( C, SGS_STAT_DUMP_STACK ); 74 | if( STREQ( D->iword, "globals" ) ) sgs_Stat( C, SGS_STAT_DUMP_GLOBALS ); 75 | if( STREQ( D->iword, "objects" ) ) sgs_Stat( C, SGS_STAT_DUMP_OBJECTS ); 76 | if( STREQ( D->iword, "callstack" ) || STREQ( D->iword, "frames" ) ) 77 | sgs_Stat( C, SGS_STAT_DUMP_FRAMES ); 78 | if( STREQ( D->iword, "exec" ) ) 79 | sgs_ExecBuffer( C, D->input.ptr + 5, D->input.size - 5 ); 80 | if( STREQ( D->iword, "eval" ) ) 81 | { 82 | int rvc = sgs_EvalBuffer( C, D->input.ptr + 5, D->input.size - 5 ); 83 | sgs_GlobalCall( C, "printvar", rvc, 0 ); 84 | } 85 | if( STREQ( D->iword, "print" ) ) 86 | { 87 | int rvc; 88 | sgs_MemBuf prepstr = sgs_membuf_create(); 89 | sgs_membuf_appbuf( &prepstr, C, "return (", 8 ); 90 | sgs_membuf_appbuf( &prepstr, C, D->input.ptr + 5, D->input.size - 5 ); 91 | sgs_membuf_appbuf( &prepstr, C, ");", 2 ); 92 | rvc = sgs_EvalBuffer( C, prepstr.ptr, prepstr.size ); 93 | sgs_membuf_destroy( &prepstr, C ); 94 | sgs_GlobalCall( C, "printvar", rvc, 0 ); 95 | } 96 | } 97 | 98 | C->sf_count += SGS_IDBG_STACK_EXTENSION; 99 | D->inside = 0; 100 | } 101 | 102 | 103 | static int idbg_stackitem( SGS_CTX ) 104 | { 105 | sgs_Bool full = 0; 106 | sgs_Int off, cnt; 107 | sgs_Variable* a, *b; 108 | SGS_IDBG = (sgs_IDbg*) C->msg_ctx; 109 | 110 | SGSFN( "dbg_stackitem" ); 111 | if( !sgs_LoadArgs( C, "i|b", &off, &full ) ) 112 | return 0; 113 | 114 | a = full ? C->stack_base : C->stack_base + D->stkoff; 115 | b = C->stack_base + D->stksize; 116 | cnt = b - a; 117 | if( off >= cnt || -off > cnt ) 118 | { 119 | sgs_Msg( C, SGS_WARNING, 120 | "index %d out of bounds, count = %d\n", (int) off, (int) cnt ); 121 | return 0; 122 | } 123 | 124 | sgs_PushVariable( C, ( off >= 0 ? a : b )[ off ] ); 125 | return 1; 126 | } 127 | 128 | static int idbg_setstackitem( SGS_CTX ) 129 | { 130 | sgs_Bool full = 0; 131 | sgs_Int off, cnt; 132 | sgs_Variable* a, *b, *x, tmp; 133 | SGS_IDBG = (sgs_IDbg*) C->msg_ctx; 134 | 135 | SGSFN( "dbg_setstackitem" ); 136 | if( !sgs_LoadArgs( C, "i?v|b", &off, &full ) ) 137 | return 0; 138 | 139 | a = full ? C->stack_base : C->stack_base + D->stkoff; 140 | b = C->stack_base + D->stksize; 141 | cnt = b - a; 142 | if( off >= cnt || -off > cnt ) 143 | { 144 | sgs_Msg( C, SGS_WARNING, 145 | "index %d out of bounds, count = %d\n", (int) off, (int) cnt ); 146 | return 0; 147 | } 148 | 149 | x = off >= 0 ? a + off : b + off; 150 | tmp = *x; 151 | sgs_Acquire( C, &tmp ); 152 | sgs_GetStackItem( C, 1, x ); 153 | sgs_Release( C, &tmp ); 154 | return 0; 155 | } 156 | 157 | 158 | int sgs_InitIDbg( SGS_CTX, SGS_IDBG ) 159 | { 160 | D->C = C; 161 | sgs_GetMsgFunc( C, &D->pfn, &D->pctx ); 162 | sgs_SetMsgFunc( C, idbgPrintFunc, D ); 163 | 164 | D->input = sgs_membuf_create(); 165 | D->iword[0] = 0; 166 | D->inside = 0; 167 | D->minlev = SGS_WARNING; 168 | 169 | sgs_SetGlobalByName( C, "dbg_stackitem", sgs_MakeCFunc( idbg_stackitem ) ); 170 | sgs_SetGlobalByName( C, "dbg_setstackitem", sgs_MakeCFunc( idbg_setstackitem ) ); 171 | 172 | return SGS_SUCCESS; 173 | } 174 | 175 | int sgs_CloseIDbg( SGS_CTX, SGS_IDBG ) 176 | { 177 | sgs_SetMsgFunc( C, D->pfn, D->pctx ); 178 | sgs_membuf_destroy( &D->input, C ); 179 | return SGS_SUCCESS; 180 | } 181 | 182 | -------------------------------------------------------------------------------- /ext/sgs_idbg.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SGS_IDBG_H_INCLUDED 3 | #define SGS_IDBG_H_INCLUDED 4 | 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | 11 | #ifndef HEADER_SGSCRIPT_H 12 | # define HEADER_SGSCRIPT_H 13 | #endif 14 | #include HEADER_SGSCRIPT_H 15 | #ifndef HEADER_SGS_UTIL_H 16 | # define HEADER_SGS_UTIL_H 17 | #endif 18 | #include HEADER_SGS_UTIL_H 19 | 20 | typedef 21 | struct _sgs_IDbg 22 | { 23 | SGS_CTX; 24 | sgs_MsgFunc pfn; 25 | void* pctx; 26 | sgs_MemBuf input; 27 | char iword[ 32 ]; 28 | int inside; 29 | ptrdiff_t stkoff; 30 | ptrdiff_t stksize; 31 | 32 | int minlev; 33 | } 34 | sgs_IDbg; 35 | 36 | #define SGS_IDBG sgs_IDbg* D 37 | 38 | int sgs_InitIDbg( SGS_CTX, SGS_IDBG ); 39 | int sgs_CloseIDbg( SGS_CTX, SGS_IDBG ); 40 | 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /ext/sgs_prof.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef SGS_PROF_H_INCLUDED 3 | #define SGS_PROF_H_INCLUDED 4 | 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | 11 | #ifndef HEADER_SGSCRIPT_H 12 | # define HEADER_SGSCRIPT_H 13 | #endif 14 | #include HEADER_SGSCRIPT_H 15 | #ifndef HEADER_SGS_UTIL_H 16 | # define HEADER_SGS_UTIL_H 17 | #endif 18 | #include HEADER_SGS_UTIL_H 19 | 20 | 21 | typedef struct _sgs_Prof 22 | { 23 | int mode; 24 | sgs_VHTable ctx2prof; 25 | /* mode 1 / 3 */ 26 | sgs_VHTable timings; 27 | /* mode 2 */ 28 | double* ictrs; 29 | uint32_t* iexcs; 30 | } 31 | sgs_Prof; 32 | 33 | typedef struct _sgs_ProfData 34 | { 35 | sgs_Prof* prof; 36 | sgs_HookFunc hfn; 37 | void* hctx; 38 | /* mode 1 / 3 */ 39 | sgs_MemBuf keytmp; 40 | sgs_MemBuf frametmp; 41 | /* mode 2 */ 42 | int prev; 43 | int32_t instr; 44 | double starttime; 45 | } 46 | sgs_ProfData; 47 | 48 | 49 | #define SGS_PROF_FUNCTIME 1 50 | #define SGS_PROF_OPTIME 2 51 | #define SGS_PROF_MEMUSAGE 3 52 | 53 | #define SGS_PROFDUMP_DATAONLY 0 /* only data and nothing else, for parsing */ 54 | #define SGS_PROFDUMP_MINREAD 1 /* the minimum readable data, for separate files */ 55 | #define SGS_PROFDUMP_FULL 2 /* with prefixes and suffixes, for finding in stdout */ 56 | 57 | 58 | /* initialize profiler and attach to context */ 59 | void sgs_ProfInit( SGS_CTX, sgs_Prof* P, int mode ); 60 | 61 | /* attach profiler to another context */ 62 | void sgs_ProfAttach( SGS_CTX, sgs_Prof* P ); 63 | 64 | /* detach profiler from context */ 65 | void sgs_ProfDetach( SGS_CTX, sgs_Prof* P ); 66 | 67 | /* close profiler, detaching from all contexts */ 68 | void sgs_ProfClose( SGS_CTX, sgs_Prof* P ); 69 | 70 | /* dump profiler measurements */ 71 | /* it's using the sgs_Write* API so output file can be overridden there */ 72 | #define sgs_ProfDump( C, P ) sgs_ProfDumpExt( C, P, SGS_PROFDUMP_FULL ) 73 | void sgs_ProfDumpExt( SGS_CTX, sgs_Prof* P, int pfxsfx ); 74 | 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /ext/sgsc.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sgscript.h" 8 | 9 | 10 | #define EPFX "SGSC Error: " 11 | #define EPRINT( x ) printf( EPFX x "\n" ) 12 | 13 | int action = 0; 14 | const char* infile = NULL; 15 | const char* outfile = NULL; 16 | const char* outext = ".sgc"; 17 | 18 | void print_help() 19 | { 20 | printf( "Available options:\n" 21 | " -h\t- print help info\n" 22 | " -c\t- compile a file\n" 23 | " -d\t- dump bytecode to STDOUT\n" 24 | " -o\t- set compiled output file name (optional)\n" 25 | "note: either -c or -d must be specified\n" 26 | "example usage:\n" 27 | "> sgsc -c script.sgs -o compiled.sgc\n" 28 | "> sgsc -d compiled.sgc\n" 29 | "\n" ); 30 | } 31 | 32 | int loadfile( const char* file, char** out, size_t* outsize ) 33 | { 34 | char* data; 35 | size_t len; 36 | long ftrv; 37 | FILE* f; 38 | 39 | f = fopen( file, "rb" ); 40 | if( !f ) 41 | return SGS_ENOTFND; 42 | fseek( f, 0, SEEK_END ); 43 | ftrv = ftell( f ); 44 | if( ftrv < 0 ) 45 | { 46 | fclose( f ); 47 | return SGS_EINPROC; 48 | } 49 | len = (size_t) ftrv; 50 | fseek( f, 0, SEEK_SET ); 51 | 52 | data = (char*) malloc( len ); 53 | if( fread( data, 1, len, f ) != len ) 54 | { 55 | fclose( f ); 56 | free( data ); 57 | return SGS_EINPROC; 58 | } 59 | fclose( f ); 60 | 61 | *out = data; 62 | *outsize = len; 63 | return SGS_SUCCESS; 64 | } 65 | 66 | int main( int argc, char** argv ) 67 | { 68 | int i; 69 | 70 | printf( "SGSC [SGScript v%s]\n", SGS_VERSION ); 71 | 72 | /* parse */ 73 | for( i = 1; i < argc; ++i ) 74 | { 75 | if( 0 == strcmp( argv[ i ], "-h" ) ) 76 | { 77 | print_help(); 78 | return 0; 79 | } 80 | else if( 0 == strcmp( argv[ i ], "-c" ) ) 81 | action = 1; 82 | else if( 0 == strcmp( argv[ i ], "-d" ) ) 83 | action = 2; 84 | else if( 0 == strcmp( argv[ i ], "-o" ) ) 85 | { 86 | i++; 87 | if( i >= argc ) 88 | { 89 | EPRINT( "expected file name after -o" ); 90 | return 1; 91 | } 92 | outfile = argv[ i ]; 93 | } 94 | else if( argv[ i ][ 0 ] == '-' ) 95 | { 96 | EPRINT( "unrecognized option" ); 97 | print_help(); 98 | return 1; 99 | } 100 | else if( infile ) 101 | { 102 | EPRINT( "can only process one file at a time" ); 103 | return 1; 104 | } 105 | else 106 | infile = argv[ i ]; 107 | } 108 | 109 | /* validate */ 110 | if( !action ) 111 | { 112 | EPRINT( "action (-c or -d) not specified" ); 113 | print_help(); 114 | return 1; 115 | } 116 | else if( !infile ) 117 | { 118 | EPRINT( "no input file" ); 119 | print_help(); 120 | return 1; 121 | } 122 | 123 | /* do */ 124 | { 125 | int ret; 126 | FILE* f; 127 | char of[ 270 ]; 128 | size_t size; 129 | char* data = NULL; 130 | sgs_Context* C = sgs_CreateEngine(); /* RSRC: sgs_CreateEngine -> C */ 131 | 132 | ret = loadfile( infile, &data, &size ); 133 | if( ret != SGS_SUCCESS ) 134 | { 135 | printf( EPFX "failed to read the file, error %d\n", ret ); 136 | sgs_DestroyEngine( C ); 137 | return ret; 138 | } 139 | /* RSRC: loadfile -> data */ 140 | 141 | if( action == 1 ) 142 | { 143 | char* data2 = NULL; 144 | size_t size2; 145 | ret = sgs_Compile( C, data, size, &data2, &size2 ); 146 | free( data ); 147 | /* FREE: loadfile */ 148 | if( ret != SGS_SUCCESS ) 149 | { 150 | printf( EPFX "failed to compile, error %d\n", ret ); 151 | sgs_DestroyEngine( C ); 152 | return ret; 153 | } 154 | /* RSRC: sgs_Compile -> data2 */ 155 | if( outfile ) 156 | strncpy( of, outfile, 260 ); 157 | else 158 | { 159 | char* sp, *pp = NULL; 160 | strncpy( of, infile, 260 ); 161 | sp = strstr( of, ".sgs" ); 162 | while( sp ) 163 | { 164 | pp = sp; 165 | sp = strstr( sp + 1, ".sgs" ); 166 | } 167 | if( pp - of + 4 == (ptrdiff_t) strlen( of ) ) 168 | memcpy( pp, ".sgc", 4 ); 169 | else 170 | strcat( of, ".sgc" ); 171 | } 172 | f = fopen( of, "wb" ); 173 | if( !f ) 174 | { 175 | sgs_Free( C, data2 ); 176 | sgs_DestroyEngine( C ); 177 | printf( EPFX "failed to open output file for writing\n" ); 178 | return errno; 179 | } 180 | /* RSRC: fopen -> f */ 181 | if( fwrite( data2, size2, 1, f ) < 1 ) 182 | { 183 | fclose( f ); 184 | sgs_Free( C, data2 ); 185 | sgs_DestroyEngine( C ); 186 | printf( EPFX "failed to write to '%s'\n", of ); 187 | return errno; 188 | } 189 | sgs_Free( C, data2 ); /* FREE: sgs_Compile */ 190 | fclose( f ); /* FREE: fopen */ 191 | 192 | printf( "successfully wrote bytecode to '%s'\n", of ); 193 | } 194 | else if( action == 2 ) 195 | { 196 | /* MAINTAIN 197 | - RSRC: loadfile -> data 198 | - RSRC: sgs_CreateEngine -> C 199 | */ 200 | ret = sgs_DumpCompiled( C, data, size ); 201 | free( data ); 202 | /* FREE: loadfile */ 203 | 204 | if( ret != SGS_SUCCESS ) 205 | { 206 | printf( EPFX "failed to dump input file, error %d\n", ret ); 207 | sgs_DestroyEngine( C ); 208 | return ret; 209 | } 210 | } 211 | 212 | sgs_DestroyEngine( C ); 213 | } 214 | 215 | return 0; 216 | } 217 | -------------------------------------------------------------------------------- /ext/sgscppbctest.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | 9 | #define SGS_CPPBC_WITH_STD_STRING 10 | #define SGS_CPPBC_WITH_STD_VECTOR 11 | 12 | #include "sgs_cppbc.h" 13 | 14 | struct Vec3 : sgsLiteObjectBase 15 | { 16 | SGS_OBJECT_LITE; 17 | 18 | Vec3( float _x, float _y, float _z ) : x(_x), y(_y), z(_z){} 19 | 20 | float _get_length() const { return (float) sqrtf(x*x+y*y+z*z); } 21 | 22 | SGS_PROPFN( RW SERIALIZE 0 ) float x; 23 | SGS_PROPFN( RW SERIALIZE 1 ) float y; 24 | SGS_PROPFN( RW SERIALIZE 2 ) float z; 25 | 26 | SGS_PROPERTY_FUNC( READ _get_length ) SGS_ALIAS( float length ); 27 | SGS_METHOD float getLength() const { return _get_length(); } 28 | SGS_METHOD void setLength( float len = 1.0f ) 29 | { 30 | if( x == 0 && y == 0 && z == 0 ) 31 | x = 1; 32 | else 33 | { 34 | float ol = _get_length(); 35 | len /= ol; 36 | } 37 | x *= len; 38 | y *= len; 39 | z *= len; 40 | } 41 | SGS_METHOD virtual void setOneVal( float v ) 42 | { 43 | x = v; 44 | y = v; 45 | z = v; 46 | } 47 | SGS_IFUNC( CONVERT ) int _convert( SGS_CTX, sgs_VarObj* data, int type ) 48 | { 49 | if( type == SGS_VT_STRING ) 50 | { 51 | char bfr[ 128 ] = {0}; 52 | snprintf( bfr, 127, "Vec3(%g;%g;%g)", x, y, z ); 53 | sgs_PushString( C, bfr ); 54 | return SGS_SUCCESS; 55 | } 56 | return SGS_ENOTSUP; 57 | } 58 | }; 59 | 60 | 61 | class Account : public sgsObjectBase 62 | { 63 | public: 64 | 65 | Account() : maybeIntTest2(42), bitfieldTest1(true) 66 | { 67 | vectorTest2.push_back( 1337 ); 68 | stdStringTest2 = "test"; 69 | } 70 | 71 | typedef sgsHandle< Account > Handle; 72 | 73 | SGS_OBJECT; 74 | 75 | SGS_PROPERTY sgsString name; 76 | SGS_PROPERTY_FUNC( READ WRITE VARNAME name2 ) SGS_ALIAS( sgsString name ); 77 | 78 | SGS_PROPERTY Handle attached; 79 | SGS_GCREF( attached ); 80 | SGS_NODUMP( attached ); 81 | SGS_PROPERTY_FUNC( READ WRITE VALIDATE attached.not_null() 82 | SOURCE attached->name ) SGS_ALIAS( sgsString attachedName ); 83 | SGS_PROPERTY sgsMaybe maybeIntTest1; 84 | SGS_PROPERTY sgsMaybe maybeIntTest2; 85 | SGS_PROPERTY bool bitfieldTest1 : 1; 86 | SGS_PROPERTY sgsVariable sgsVarTest1; 87 | SGS_PROPERTY Handle handleTest1; 88 | std::vector< int > vectorTest1; SGS_DUMP( vectorTest1 ); 89 | std::vector< int > vectorTest2; SGS_DUMP( vectorTest2 ); 90 | SGS_PROPERTY std::string stdStringTest1; 91 | SGS_PROPERTY std::string stdStringTest2; 92 | SGS_METHOD bool returnsTrue(){ return true; } 93 | 94 | /* `Handle` must be resolved since it's going to be used out of scope */ 95 | SGS_METHOD bool sendMoney( Account::Handle to, float amount, sgsString currency ) 96 | { 97 | if( !to.not_null() ) 98 | { 99 | printf( "--- sending money ---\n! recipient not specified\n" ); 100 | return false; 101 | } 102 | printf( "--- sending money ---\nfrom: %.*s\nto: %.*s\namount: %.4f\ncurrency: %.*s\n---\n", 103 | (int) name.size(), name.c_str(), 104 | (int) to->name.size(), to->name.c_str(), 105 | amount, 106 | (int) currency.size(), currency.c_str() ); 107 | return true; 108 | } 109 | 110 | // purposefully compacted formatting 111 | SGS_METHOD_NAMED( coroAware ) int sgsCoroAware(int a,SGS_CTX,int b,sgs_Context* c,int d){ 112 | return a + b + ( C == c ) + ( C == this->C ) + d; 113 | } 114 | 115 | SGS_IFUNC( CONVERT ) int _convert( SGS_CTX, sgs_VarObj* data, int type ) 116 | { 117 | if( type == SGS_VT_STRING ) 118 | { 119 | sgs_PushString( C, "Account(" ); 120 | name.push( C ); 121 | sgs_PushString( C, ")" ); 122 | sgs_StringConcat( C, 3 ); 123 | return SGS_SUCCESS; 124 | } 125 | return SGS_ENOTSUP; 126 | } 127 | }; 128 | 129 | struct Padding 130 | { 131 | int _padding; 132 | }; 133 | 134 | class AccountExt : public Padding, public Account 135 | { 136 | public: 137 | 138 | typedef sgsHandle< AccountExt > Handle; 139 | 140 | SGS_OBJECT_INHERIT( Account ); 141 | 142 | SGS_PROPERTY sgsString nameExt; 143 | }; 144 | 145 | 146 | struct XRef : sgsObjectBase 147 | { 148 | typedef sgsHandle< XRef > Handle; 149 | SGS_OBJECT; 150 | SGS_PROPERTY Handle other; 151 | SGS_PROPERTY Handle other2; 152 | 153 | static int recreate( SGS_CTX ) 154 | { 155 | SGS_CREATECLASS( C, NULL, XRef, () ); 156 | return 1; 157 | } 158 | SGS_UNSERIALIZE_FUNC( recreate ); 159 | 160 | ~XRef() 161 | { 162 | printf( "XRef(obj=%p, rc=%d) dtor\n", m_sgsObject, int(m_sgsObject->refcount) ); 163 | if( other.object ) 164 | { 165 | printf( "- has other(obj=%p, rc=%d)\n", other.object, int(other.object->refcount) ); 166 | } 167 | if( other2.object ) 168 | { 169 | printf( "- has other2(obj=%p, rc=%d)\n", other2.object, int(other2.object->refcount) ); 170 | } 171 | } 172 | }; 173 | 174 | -------------------------------------------------------------------------------- /ext/sgsdemo.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | SGScript HTML5 demo 5 | 6 | 36 | 40 | 41 | 42 |
You can test SGScript here.
43 | 44 | 45 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
46 | 47 |
Input:Output:
56 | 57 | -------------------------------------------------------------------------------- /ext/sgsexe.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | 6 | #define APPNAME "SGSVM Executable Stub" 7 | 8 | 9 | #define FILE_READ 1 10 | #define FILE_WRITE 2 11 | HANDLE CreateFileFast( const CHAR* name, DWORD rw, DWORD mode ) 12 | { 13 | DWORD access = 0; 14 | DWORD share = 0; 15 | if( rw & FILE_READ ) 16 | { 17 | access |= GENERIC_READ; 18 | share |= FILE_SHARE_READ; 19 | } 20 | if( rw & FILE_WRITE ) 21 | { 22 | access |= GENERIC_WRITE; 23 | share |= FILE_SHARE_WRITE; 24 | } 25 | return CreateFile( name, access, share, NULL, mode, FILE_ATTRIBUTE_NORMAL, NULL ); 26 | } 27 | 28 | 29 | int main( int argc, char* argv[] ) 30 | { 31 | int i; 32 | BYTE buf[ 4096 ], *path, *data; 33 | DWORD read, written, scriptsize = 0; 34 | HANDLE fh; 35 | 36 | path = buf; 37 | 38 | /* open EXE file */ 39 | read = GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) ); 40 | if( read >= sizeof( buf ) ) 41 | { 42 | path = malloc( read + 1 ); 43 | GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) ); 44 | } 45 | fh = CreateFileFast( (CHAR*) buf, FILE_READ, OPEN_EXISTING ); 46 | if( path != buf ) 47 | free( path ); 48 | if( fh == INVALID_HANDLE_VALUE ) 49 | return E_FAIL; 50 | SetFilePointer( fh, -4, NULL, FILE_END ); 51 | ReadFile( fh, &scriptsize, sizeof( scriptsize ), &read, NULL ); 52 | 53 | /* read the following data */ 54 | if( scriptsize == 0 ) 55 | { 56 | if( argc == 3 ) 57 | { 58 | HANDLE hsgs, hout = CreateFileFast( argv[ 1 ], FILE_WRITE, CREATE_ALWAYS ); 59 | if( !hout ) 60 | { 61 | MessageBox( 0, "Could not open executable file for writing", APPNAME, MB_ICONERROR ); 62 | CloseHandle( fh ); 63 | return E_FAIL; 64 | } 65 | hsgs = CreateFileFast( argv[ 2 ], FILE_READ, OPEN_EXISTING ); 66 | if( !hsgs ) 67 | { 68 | MessageBox( 0, "Could not open script file for reading", APPNAME, MB_ICONERROR ); 69 | CloseHandle( hout ); 70 | CloseHandle( fh ); 71 | return E_FAIL; 72 | } 73 | SetFilePointer( fh, 0, NULL, FILE_BEGIN ); 74 | while( ReadFile( fh, buf, sizeof( buf ), &read, NULL ) && read ) 75 | WriteFile( hout, buf, read, &written, NULL ); 76 | while( ReadFile( hsgs, buf, sizeof( buf ), &read, NULL ) && read ) 77 | WriteFile( hout, buf, read, &written, NULL ); 78 | scriptsize = GetFileSize( hsgs, NULL ); 79 | WriteFile( hout, &scriptsize, 4, &written, NULL ); 80 | CloseHandle( fh ); 81 | CloseHandle( hsgs ); 82 | CloseHandle( hout ); 83 | MessageBox( 0, "File saved!", APPNAME, MB_ICONINFORMATION ); 84 | return S_OK; 85 | } 86 | else 87 | { 88 | const char* info = "To create an executable from .sgs" 89 | ", run sgsexe ." 90 | "\n\nglobal 'argv' will be the array of arguments"; 91 | MessageBox( 0, info, APPNAME, MB_ICONINFORMATION ); 92 | CloseHandle( fh ); 93 | return E_ABORT; 94 | } 95 | } 96 | 97 | data = malloc( scriptsize ); 98 | SetFilePointer( fh, -4-(LONG)scriptsize, NULL, FILE_END ); 99 | ReadFile( fh, data, scriptsize, &read, NULL ); 100 | CloseHandle( fh ); 101 | 102 | { 103 | SGS_CTX = sgs_CreateEngine(); 104 | 105 | for( i = 0; i < argc; ++i ) 106 | sgs_PushString( C, argv[ i ] ); 107 | 108 | sgs_CreateArray( C, NULL, argc ); 109 | sgs_SetGlobalByName( C, "argv", sgs_StackItem( C, -1 ) ); 110 | sgs_Pop( C, 1 ); 111 | 112 | sgs_SetGlobalByName( C, "argc", sgs_MakeInt( argc ) ); 113 | 114 | sgs_ExecBuffer( C, (char*) data, scriptsize ); 115 | 116 | sgs_DestroyEngine( C ); 117 | } 118 | 119 | free( data ); 120 | 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /ext/sgsvm.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include "sgscript.h" 6 | #include "sgs_dbgserver.h" 7 | #include "sgs_prof.h" 8 | 9 | 10 | #define EPFX "SGSVM Error: " 11 | #define EPRINT( x ) printf( EPFX x "\n" ) 12 | 13 | void readme() 14 | { 15 | puts( "syntax:" ); 16 | puts( " sgsvm [files|options]" ); 17 | puts( " sgsvm [options] -p [, ]" ); 18 | puts( "" ); 19 | puts( "options:" ); 20 | puts( " -h, --help: print this text" ); 21 | puts( " -v, --version: print version info" ); 22 | puts( " -s, --separate: restart the engine between scripts" ); 23 | puts( " -d, --debug: enable interactive debugging on errors" ); 24 | puts( " -dsp, --debug-start-paused: start from debugger before running any code" ); 25 | puts( " -p, --program: translate the following arguments into a SGS program call" ); 26 | puts( " --stats: print VM stats after running the scripts" ); 27 | puts( " --profile: enable profiling by collecting call stack timings" ); 28 | puts( " --profile-ops: enable low-level VM instruction profiling" ); 29 | puts( " --profile-mem: enable memory usage profiling" ); 30 | } 31 | 32 | int sep = 0, v = 0; 33 | sgs_Context* C; 34 | sgs_DebugServer* D; 35 | sgs_Prof P; 36 | int idbg = 0; 37 | int idbg_break_on_start = 0; 38 | int prof = 0; 39 | int stats = 0; 40 | 41 | void sgs_init() 42 | { 43 | C = sgs_CreateEngine(); 44 | if( idbg ) 45 | { 46 | D = sgs_CreateDebugServer( C, 0 ); 47 | if( idbg_break_on_start ) 48 | sgs_DebugServerCmd( D, NULL ); 49 | } 50 | if( prof ) sgs_ProfInit( C, &P, prof ); 51 | } 52 | void sgs_close() 53 | { 54 | if( idbg ) sgs_CloseDebugServer( D ); 55 | if( prof ) 56 | { 57 | sgs_ProfDump( C, &P ); 58 | sgs_ProfClose( C, &P ); 59 | } 60 | if( stats ) 61 | sgs_Stat( C, SGS_STAT_DUMP_STATS ); 62 | sgs_DestroyEngine( C ); 63 | } 64 | void sgs_dofile( const char* name, int incl ) 65 | { 66 | int rv = incl ? sgs_Include( C, name ) : sgs_ExecFile( C, name ); 67 | 68 | if( rv < 0 ) 69 | { 70 | if( rv == SGS_ENOTFND ) printf( EPFX "file was not found: %s\n", name ); 71 | else if( rv == SGS_EINPROC ) printf( EPFX "failed to load file: %s\n", name ); 72 | else printf( EPFX "failed to run file: %s\n", name ); 73 | } 74 | } 75 | void sgs_printversion() 76 | { 77 | if( v ) 78 | printf( "SGSVM [SGScript v%s]\n", SGS_VERSION ); 79 | } 80 | 81 | int main( int argc, char** argv ) 82 | { 83 | int i, j; 84 | 85 | if( argc < 2 ) 86 | { 87 | EPRINT( "need to specify at least one file" ); 88 | readme(); 89 | return 1; 90 | } 91 | 92 | for( i = 1; i < argc; ++i ) 93 | { 94 | if( strcmp( argv[ i ], "--separate" ) == 0 || 95 | strcmp( argv[ i ], "-s" ) == 0 ){ sep = 1; argv[ i ] = 0; } 96 | else if( strcmp( argv[ i ], "--debug" ) == 0 || 97 | strcmp( argv[ i ], "-d" ) == 0 ){ idbg = 1; argv[ i ] = 0; } 98 | else if( strcmp( argv[ i ], "--debug-start-paused" ) == 0 || 99 | strcmp( argv[ i ], "-dsp" ) == 0 ){ idbg = 1; idbg_break_on_start = 1; argv[ i ] = 0; } 100 | else if( strcmp( argv[ i ], "--profile" ) == 0 ) 101 | { prof = 1; argv[ i ] = 0; } 102 | else if( strcmp( argv[ i ], "--profile-ops" ) == 0 ) 103 | { prof = 2; argv[ i ] = 0; } 104 | else if( strcmp( argv[ i ], "--profile-mem" ) == 0 ) 105 | { prof = 3; argv[ i ] = 0; } 106 | else if( strcmp( argv[ i ], "--help" ) == 0 || 107 | strcmp( argv[ i ], "-h" ) == 0 ){ readme(); return 0; } 108 | else if( strcmp( argv[ i ], "--version" ) == 0 || 109 | strcmp( argv[ i ], "-v" ) == 0 ){ v = 1; argv[ i ] = 0; } 110 | else if( strcmp( argv[ i ], "--stats" ) == 0 ){ stats = 1; argv[ i ] = 0; } 111 | else if( strcmp( argv[ i ], "--program" ) == 0 || 112 | strcmp( argv[ i ], "-p" ) == 0 ) 113 | { 114 | i++; 115 | if( i == argc ) 116 | { 117 | EPRINT( "file name expected" ); 118 | return 1; 119 | } 120 | 121 | sgs_printversion(); 122 | sgs_init(); 123 | 124 | for( j = i; j < argc; ++j ) 125 | sgs_PushString( C, argv[ j ] ); 126 | sgs_CreateArray( C, NULL, argc - i ); 127 | sgs_SetGlobalByName( C, "argv", sgs_StackItem( C, -1 ) ); 128 | sgs_Pop( C, 1 ); 129 | 130 | sgs_SetGlobalByName( C, "argc", sgs_MakeInt( argc - i ) ); 131 | 132 | sgs_dofile( argv[ i ], 1 ); 133 | sgs_close(); 134 | return 0; 135 | } 136 | } 137 | 138 | sgs_printversion(); 139 | 140 | sgs_init(); 141 | for( i = 1; i < argc; ++i ) 142 | { 143 | if( argv[ i ] ) 144 | { 145 | sgs_dofile( argv[ i ], 0 ); 146 | 147 | if( sep ) 148 | { 149 | sgs_close(); 150 | sgs_init(); 151 | } 152 | } 153 | } 154 | 155 | sgs_close(); 156 | return 0; 157 | } 158 | -------------------------------------------------------------------------------- /ext/sgsxgmath.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef SGSXGMATH_H 4 | #define SGSXGMATH_H 5 | 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | 12 | #include 13 | #include 14 | 15 | #ifndef HEADER_SGSCRIPT_H 16 | # define HEADER_SGSCRIPT_H 17 | #endif 18 | #include HEADER_SGSCRIPT_H 19 | 20 | 21 | #ifndef XGM_VT 22 | #define XGM_VT float 23 | #endif 24 | #ifndef XGM_PROPTYPE 25 | #define XGM_PROPTYPE SGS_OBJPROPTYPE_FLOAT 26 | #endif 27 | 28 | #define XGM_SMALL_VT 0.0001f 29 | 30 | 31 | typedef struct _xgm_vtarray 32 | { 33 | XGM_VT* data; 34 | sgs_SizeVal size; 35 | sgs_SizeVal mem; 36 | } 37 | xgm_vtarray; 38 | 39 | 40 | SGS_APIFUNC extern sgs_ObjInterface xgm_vec2_iface[1]; 41 | SGS_APIFUNC extern sgs_ObjInterface xgm_vec3_iface[1]; 42 | SGS_APIFUNC extern sgs_ObjInterface xgm_vec4_iface[1]; 43 | SGS_APIFUNC extern sgs_ObjInterface xgm_aabb2_iface[1]; 44 | SGS_APIFUNC extern sgs_ObjInterface xgm_aabb3_iface[1]; 45 | SGS_APIFUNC extern sgs_ObjInterface xgm_color_iface[1]; 46 | SGS_APIFUNC extern sgs_ObjInterface xgm_quat_iface[1]; 47 | SGS_APIFUNC extern sgs_ObjInterface xgm_mat3_iface[1]; 48 | SGS_APIFUNC extern sgs_ObjInterface xgm_mat4_iface[1]; 49 | SGS_APIFUNC extern sgs_ObjInterface xgm_floatarr_iface[1]; 50 | 51 | SGS_APIFUNC SGSONE sgs_CreateVec2( SGS_CTX, sgs_Variable* var, XGM_VT x, XGM_VT y ); 52 | SGS_APIFUNC SGSONE sgs_CreateVec3( SGS_CTX, sgs_Variable* var, XGM_VT x, XGM_VT y, XGM_VT z ); 53 | SGS_APIFUNC SGSONE sgs_CreateVec4( SGS_CTX, sgs_Variable* var, XGM_VT x, XGM_VT y, XGM_VT z, XGM_VT w ); 54 | SGS_APIFUNC SGSONE sgs_CreateAABB2( SGS_CTX, sgs_Variable* var, XGM_VT x1, XGM_VT y1, XGM_VT x2, XGM_VT y2 ); 55 | SGS_APIFUNC SGSONE sgs_CreateAABB3( SGS_CTX, sgs_Variable* var, const XGM_VT* v3a, const XGM_VT* v3b ); 56 | SGS_APIFUNC SGSONE sgs_CreateColor( SGS_CTX, sgs_Variable* var, XGM_VT r, XGM_VT g, XGM_VT b, XGM_VT a ); 57 | SGS_APIFUNC SGSONE sgs_CreateQuat( SGS_CTX, sgs_Variable* var, XGM_VT x, XGM_VT y, XGM_VT z, XGM_VT w ); 58 | SGS_APIFUNC SGSONE sgs_CreateMat3( SGS_CTX, sgs_Variable* var, const XGM_VT* v9f, int transpose ); 59 | SGS_APIFUNC SGSONE sgs_CreateMat4( SGS_CTX, sgs_Variable* var, const XGM_VT* v16f, int transpose ); 60 | SGS_APIFUNC SGSONE sgs_CreateFloatArray( SGS_CTX, sgs_Variable* var, const XGM_VT* vfn, sgs_SizeVal size ); 61 | 62 | SGS_APIFUNC SGSONE sgs_CreateVec2p( SGS_CTX, sgs_Variable* var, const XGM_VT* v2f ); 63 | SGS_APIFUNC SGSONE sgs_CreateVec3p( SGS_CTX, sgs_Variable* var, const XGM_VT* v3f ); 64 | SGS_APIFUNC SGSONE sgs_CreateVec4p( SGS_CTX, sgs_Variable* var, const XGM_VT* v4f ); 65 | SGS_APIFUNC SGSONE sgs_CreateAABB2p( SGS_CTX, sgs_Variable* var, const XGM_VT* v4f ); 66 | SGS_APIFUNC SGSONE sgs_CreateAABB3p( SGS_CTX, sgs_Variable* var, const XGM_VT* v6f ); 67 | SGS_APIFUNC SGSONE sgs_CreateColorp( SGS_CTX, sgs_Variable* var, const XGM_VT* v4f ); 68 | SGS_APIFUNC SGSONE sgs_CreateColorvp( SGS_CTX, sgs_Variable* var, const XGM_VT* vf, int numfloats ); 69 | SGS_APIFUNC SGSONE sgs_CreateQuatp( SGS_CTX, sgs_Variable* var, const XGM_VT* v4f ); 70 | 71 | SGS_APIFUNC SGSBOOL sgs_ParseVT( SGS_CTX, sgs_StkIdx item, XGM_VT* out ); 72 | SGS_APIFUNC SGSBOOL sgs_ParseVec2( SGS_CTX, sgs_StkIdx item, XGM_VT* v2f, int strict ); 73 | SGS_APIFUNC SGSBOOL sgs_ParseVec3( SGS_CTX, sgs_StkIdx item, XGM_VT* v3f, int strict ); 74 | SGS_APIFUNC SGSBOOL sgs_ParseVec4( SGS_CTX, sgs_StkIdx item, XGM_VT* v4f, int strict ); 75 | SGS_APIFUNC SGSBOOL sgs_ParseAABB2( SGS_CTX, sgs_StkIdx item, XGM_VT* v4f ); 76 | SGS_APIFUNC SGSBOOL sgs_ParseAABB3( SGS_CTX, sgs_StkIdx item, XGM_VT* v6f ); 77 | SGS_APIFUNC SGSBOOL sgs_ParseColor( SGS_CTX, sgs_StkIdx item, XGM_VT* v4f, int strict ); 78 | SGS_APIFUNC SGSBOOL sgs_ParseQuat( SGS_CTX, sgs_StkIdx item, XGM_VT* v4f, int strict ); 79 | SGS_APIFUNC SGSBOOL sgs_ParseMat3( SGS_CTX, sgs_StkIdx item, XGM_VT* v9f ); 80 | SGS_APIFUNC SGSBOOL sgs_ParseMat4( SGS_CTX, sgs_StkIdx item, XGM_VT* v16f ); 81 | SGS_APIFUNC SGSBOOL sgs_ParseFloatArray( SGS_CTX, sgs_StkIdx item, XGM_VT** vfa, sgs_SizeVal* osz ); 82 | 83 | SGS_APIFUNC int sgs_ArgCheck_Vec2( SGS_CTX, int argid, va_list* args, int flags ); 84 | SGS_APIFUNC int sgs_ArgCheck_Vec3( SGS_CTX, int argid, va_list* args, int flags ); 85 | SGS_APIFUNC int sgs_ArgCheck_Vec4( SGS_CTX, int argid, va_list* args, int flags ); 86 | SGS_APIFUNC int sgs_ArgCheck_AABB2( SGS_CTX, int argid, va_list* args, int flags ); 87 | SGS_APIFUNC int sgs_ArgCheck_AABB3( SGS_CTX, int argid, va_list* args, int flags ); 88 | SGS_APIFUNC int sgs_ArgCheck_Color( SGS_CTX, int argid, va_list* args, int flags ); 89 | SGS_APIFUNC int sgs_ArgCheck_Quat( SGS_CTX, int argid, va_list* args, int flags ); 90 | SGS_APIFUNC int sgs_ArgCheck_Mat3( SGS_CTX, int argid, va_list* args, int flags ); 91 | SGS_APIFUNC int sgs_ArgCheck_Mat4( SGS_CTX, int argid, va_list* args, int flags ); 92 | SGS_APIFUNC int sgs_ArgCheck_FloatArray( SGS_CTX, int argid, va_list* args, int flags ); 93 | 94 | 95 | SGS_APIFUNC int sgs_xgm_module_entry_point( SGS_CTX ); 96 | 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | 102 | #endif /* SGSXGMATH_H */ 103 | 104 | -------------------------------------------------------------------------------- /ext/stubapp.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /jni/Android.mk: -------------------------------------------------------------------------------- 1 | 2 | LOCAL_PATH := $(call my-dir) 3 | 4 | # SGScript core library 5 | include $(CLEAR_VARS) 6 | LOCAL_MODULE := sgscript 7 | LOCAL_SRC_FILES := \ 8 | ../src/sgs_util.c \ 9 | ../src/sgs_tok.c \ 10 | ../src/sgs_ctx.c \ 11 | ../src/sgs_fnt.c \ 12 | ../src/sgs_bcg.c \ 13 | ../src/sgs_xpc.c \ 14 | ../src/sgs_proc.c \ 15 | ../src/sgs_regex.c \ 16 | ../src/sgs_std.c \ 17 | ../src/sgs_stdL.c \ 18 | ../src/sgs_srlz.c 19 | include $(BUILD_SHARED_LIBRARY) 20 | 21 | 22 | # extended game math library 23 | include $(CLEAR_VARS) 24 | LOCAL_MODULE := sgsxgmath 25 | LOCAL_CFLAGS := -DSGS_COMPILE_MODULE=1 26 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 27 | LOCAL_SRC_FILES := ../ext/sgsxgmath.c 28 | LOCAL_SHARED_LIBRARIES := sgscript 29 | include $(BUILD_SHARED_LIBRARY) 30 | 31 | 32 | # JSON library 33 | include $(CLEAR_VARS) 34 | LOCAL_MODULE := sgsjson 35 | LOCAL_CFLAGS := -DSGS_COMPILE_MODULE=1 36 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 37 | LOCAL_SRC_FILES := ../ext/sgsjson.c 38 | LOCAL_SHARED_LIBRARIES := sgscript 39 | include $(BUILD_SHARED_LIBRARY) 40 | 41 | 42 | # parallel processing library 43 | include $(CLEAR_VARS) 44 | LOCAL_MODULE := sgspproc 45 | LOCAL_CFLAGS := -DSGS_COMPILE_MODULE=1 46 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 47 | LOCAL_SRC_FILES := ../ext/sgspproc.c 48 | LOCAL_SHARED_LIBRARIES := sgscript 49 | include $(BUILD_SHARED_LIBRARY) 50 | 51 | 52 | # sockets library 53 | include $(CLEAR_VARS) 54 | LOCAL_MODULE := sgssockets 55 | LOCAL_CFLAGS := -DSGS_COMPILE_MODULE=1 56 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 57 | LOCAL_SRC_FILES := ../ext/sgssockets.c 58 | LOCAL_SHARED_LIBRARIES := sgscript 59 | include $(BUILD_SHARED_LIBRARY) 60 | 61 | 62 | # metaprogramming helper library 63 | include $(CLEAR_VARS) 64 | LOCAL_MODULE := sgsmeta 65 | LOCAL_CFLAGS := -DSGS_COMPILE_MODULE=1 66 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 67 | LOCAL_SRC_FILES := ../ext/sgsmeta.c 68 | LOCAL_SHARED_LIBRARIES := sgscript 69 | include $(BUILD_SHARED_LIBRARY) 70 | 71 | 72 | # tests 73 | ifneq ($(TEST),) 74 | 75 | include $(CLEAR_VARS) 76 | LOCAL_MODULE := sgsapitest 77 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 78 | LOCAL_SRC_FILES := ../ext/sgsapitest.c ../ext/sgs_prof.c 79 | LOCAL_SHARED_LIBRARIES := sgscript 80 | include $(BUILD_EXECUTABLE) 81 | 82 | include $(CLEAR_VARS) 83 | LOCAL_MODULE := sgstest 84 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 85 | LOCAL_SRC_FILES := ../ext/sgstest.c 86 | LOCAL_SHARED_LIBRARIES := sgscript 87 | include $(BUILD_EXECUTABLE) 88 | 89 | include $(CLEAR_VARS) 90 | LOCAL_MODULE := sgscppbctest 91 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 92 | LOCAL_CPPFLAGS += -std=c++03 93 | # need to run the local cppbctest before this to generated obj/cppbc_test.cpp 94 | LOCAL_SRC_FILES := ../ext/sgscppbctest.cpp ../obj/cppbc_test.cpp 95 | LOCAL_SHARED_LIBRARIES := sgscript 96 | include $(BUILD_EXECUTABLE) 97 | 98 | include $(CLEAR_VARS) 99 | LOCAL_MODULE := sgscppbctest11 100 | LOCAL_C_INCLUDES += $(LOCAL_PATH)/../src 101 | LOCAL_CPPFLAGS += -std=c++11 102 | # need to run the local cppbctest before this to generated obj/cppbc_test.cpp 103 | LOCAL_SRC_FILES := ../ext/sgscppbctest.cpp ../obj/cppbc_test.cpp 104 | LOCAL_SHARED_LIBRARIES := sgscript 105 | include $(BUILD_EXECUTABLE) 106 | 107 | endif 108 | -------------------------------------------------------------------------------- /jni/Application.mk: -------------------------------------------------------------------------------- 1 | APP_STL := stlport_static 2 | -------------------------------------------------------------------------------- /jni/test_android.sgs: -------------------------------------------------------------------------------- 1 | 2 | include "io", "os", "string"; 3 | 4 | if( os_gettype() == "Windows" ) 5 | { 6 | global DS = "\\"; 7 | } 8 | else 9 | { 10 | global DS = "/"; 11 | } 12 | 13 | 14 | ANDROID_SDK = os_getenv( "ANDROID_SDK" ); 15 | if( ANDROID_SDK === null ) 16 | { 17 | errprintln( "environment variable ANDROID_SDK is not set" ); 18 | app_exit( 1 ); 19 | } 20 | 21 | ANDROID_NDK = os_getenv( "ANDROID_NDK" ); 22 | if( ANDROID_NDK === null ) 23 | { 24 | errprintln( "environment variable ANDROID_NDK is not set" ); 25 | app_exit( 1 ); 26 | } 27 | 28 | ADB = ANDROID_SDK .. DS .. "platform-tools" .. DS .. "adb "; 29 | 30 | 31 | if( io_dir_exists( "jni" ) == false ) 32 | { 33 | errprintln( "run this script from root cwd" ); 34 | app_exit( 1 ); 35 | } 36 | 37 | if( os_getenv( "APP_ABI" ) === null ) 38 | os_putenv( "APP_ABI=armeabi" ); 39 | ABI = os_getenv( "APP_ABI" ); 40 | 41 | println( "> ABI: " .. ABI ); 42 | println( "> Building Android version with tests" ); 43 | os_command( ANDROID_NDK .. DS .. "ndk-build TEST=1 APP_ABI=" .. ABI ); 44 | 45 | 46 | core_files = 47 | [ 48 | "libsgscript.so", 49 | "libsgsjson.so", 50 | "libsgsmeta.so", 51 | "libsgspproc.so", 52 | "libsgsxgmath.so", 53 | "sgsapitest", 54 | "sgstest", 55 | "sgscppbctest", 56 | "sgscppbctest11", 57 | ]; 58 | 59 | 60 | println( "> Cleanup" ); 61 | foreach( f : core_files ) 62 | { 63 | os_command( ADB .. "shell rm /data/local/tmp/" .. f ); 64 | } 65 | os_command( ADB .. "shell rm -r /data/local/tmp/tests" ); 66 | 67 | println( "> Uploading test files" ); 68 | foreach( f : core_files ) 69 | { 70 | os_command( ADB .. "push " .. "libs" .. DS .. ABI .. DS .. f .. " /data/local/tmp" ); 71 | } 72 | foreach( f : io_dir( "tests" ) ) 73 | { 74 | if( string_find( f, ".sgs", f.length - 4 ) === null ) 75 | continue; 76 | os_command( ADB .. "push tests" .. DS .. f .. " /data/local/tmp/tests/" .. f ); 77 | } 78 | 79 | println( "> chmod the root script (make executable)" ); 80 | os_command( ADB .. "shell chmod 755 /data/local/tmp/test_runner.sh" ); 81 | 82 | println( "> Running tests" ); 83 | os_command( ADB .. "shell \"" 84 | .. "cd /data/local/tmp && " 85 | .. "chmod 755 sgsapitest && " 86 | .. "chmod 755 sgstest && " 87 | .. "chmod 755 sgscppbctest && " 88 | .. "chmod 755 sgscppbctest11 && " 89 | .. "mkdir tests/data && " 90 | .. "export LD_LIBRARY_PATH=. && " 91 | .. "echo '' > sgstests.log && " 92 | .. "./sgsapitest >> sgstests.log && " 93 | .. "./sgstest >> sgstests.log && " 94 | .. "./sgscppbctest >> sgstests.log && " 95 | .. "./sgscppbctest11 >> sgstests.log\"" ); 96 | 97 | println( "> Download the result logs" ); 98 | os_command( ADB .. "pull /data/local/tmp/sgstests.log" ); 99 | 100 | log = io_file_read( "sgstests.log" ); 101 | println( log ); 102 | 103 | s_api = string_find( log, "[sgsapitest] all tests were successful" ) !== null; 104 | s_main = string_find( log, "/// Tests failed: 0 / 28" ) !== null; 105 | s_cppbc = string_find( log, "[cppbc] SUCCESS!" ) !== null; 106 | s_cppbc11 = string_find( log, "[cppbc11] SUCCESS!" ) !== null; 107 | println( "===================" ); 108 | println( "API TESTS: " .. if( s_api, "[+]", "[ERROR]" ) ); 109 | println( "MAIN TESTS: " .. if( s_main, "[+]", "[ERROR]" ) ); 110 | println( "C++BC TESTS: " .. if( s_cppbc, "[+]", "[ERROR]" ) ); 111 | println( "C++BC 11 TESTS: " .. if( s_cppbc11, "[+]", "[ERROR]" ) ); 112 | println( "===================" ); 113 | -------------------------------------------------------------------------------- /lib/output.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archo5/sgscript/980a513db16b63ce65bd84c6f5084e4af7376379/lib/output.dir -------------------------------------------------------------------------------- /obj/output.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archo5/sgscript/980a513db16b63ce65bd84c6f5084e4af7376379/obj/output.dir -------------------------------------------------------------------------------- /src/sgs_regex.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef SG_REGEX_H_ 4 | #define SG_REGEX_H_ 5 | 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #include 12 | 13 | 14 | #define RXSUCCESS 0 15 | #define RXEINMOD -1 /* invalid modifier */ 16 | #define RXEPART -2 /* partial (sub-)expression */ 17 | #define RXEUNEXP -3 /* unexpected character */ 18 | #define RXERANGE -4 /* invalid range (min > max) */ 19 | #define RXELIMIT -5 /* too many digits */ 20 | #define RXEEMPTY -6 /* expression is effectively empty */ 21 | #define RXENOREF -7 /* the specified backreference cannot be used here */ 22 | 23 | #define RX_ALLMODS "mis" 24 | 25 | #ifndef RX_STRLENGTHFUNC 26 | #define RX_STRLENGTHFUNC( str ) strlen( str ) 27 | #endif 28 | 29 | 30 | typedef void* (*srx_MemFunc) 31 | ( 32 | void* /* userdata */, 33 | void* /* ptr */, 34 | size_t /* size */ 35 | ); 36 | 37 | #ifdef RX_NEED_DEFAULT_MEMFUNC 38 | static void* srx_DefaultMemFunc( void* userdata, void* ptr, size_t size ) 39 | { 40 | (void) userdata; 41 | if( size ) 42 | return realloc( ptr, size ); 43 | free( ptr ); 44 | return NULL; 45 | } 46 | #endif 47 | 48 | 49 | typedef char rxChar; 50 | typedef unsigned char rxUChar; 51 | typedef struct _srx_Context srx_Context; 52 | 53 | 54 | srx_Context* srx_CreateExt( const rxChar* str, size_t strsize, const rxChar* mods, int* errnpos, srx_MemFunc memfn, void* memctx ); 55 | #define srx_Create( str, mods ) srx_CreateExt( str, RX_STRLENGTHFUNC(str), mods, NULL, NULL, NULL ) 56 | void srx_Destroy( srx_Context* R ); 57 | void srx_DumpToFile( srx_Context* R, FILE* fp ); 58 | #define srx_DumpToStdout( R ) srx_DumpToFile( R, stdout ) 59 | 60 | int srx_MatchExt( srx_Context* R, const rxChar* str, size_t size, size_t offset ); 61 | #define srx_Match( R, str, off ) srx_MatchExt( R, str, RX_STRLENGTHFUNC(str), off ) 62 | int srx_GetCaptureCount( srx_Context* R ); 63 | int srx_GetCaptured( srx_Context* R, int which, size_t* pbeg, size_t* pend ); 64 | int srx_GetCapturedPtrs( srx_Context* R, int which, const rxChar** pbeg, const rxChar** pend ); 65 | 66 | rxChar* srx_ReplaceExt( srx_Context* R, const rxChar* str, size_t strsize, const rxChar* rep, size_t repsize, size_t* outsize ); 67 | #define srx_Replace( R, str, rep ) srx_ReplaceExt( R, str, RX_STRLENGTHFUNC(str), rep, RX_STRLENGTHFUNC(rep), NULL ) 68 | void srx_FreeReplaced( srx_Context* R, rxChar* repstr ); 69 | 70 | 71 | #ifdef __cplusplus 72 | } 73 | #endif 74 | 75 | 76 | #endif /* SG_REGEX_H_ */ 77 | 78 | -------------------------------------------------------------------------------- /tests/125-boolops_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global OPS; 3 | var A = false, B = true, C; 4 | beet = "boolean test: "; 5 | 6 | function OP_FALSE() 7 | { 8 | global OPS; 9 | OPS $= "F"; 10 | return false; 11 | } 12 | 13 | function OP_TRUE() 14 | { 15 | global OPS; 16 | OPS $= "T"; 17 | return true; 18 | } 19 | 20 | function HIT() 21 | { 22 | global OPS; 23 | OPS $= "H"; 24 | } 25 | 26 | 27 | print( "\n\tboolean assignment tests\n" ); 28 | 29 | C = A; C &&= B; testEqual( C, false, beet$ "false &&= true" ); 30 | C = A; C &&= A; testEqual( C, false, beet$ "false &&= false" ); 31 | C = B; C &&= B; testEqual( C, true, beet$ "true &&= true" ); 32 | C = B; C &&= A; testEqual( C, false, beet$ "true &&= false" ); 33 | 34 | C = A; C ||= B; testEqual( C, true, beet$ "false ||= true" ); 35 | C = A; C ||= A; testEqual( C, false, beet$ "false ||= false" ); 36 | C = B; C ||= B; testEqual( C, true, beet$ "true ||= true" ); 37 | C = B; C ||= A; testEqual( C, true, beet$ "true ||= false" ); 38 | 39 | 40 | print( "\n\tboolean tests with 2 operands\n" ); 41 | 42 | OPS = ""; if( OP_FALSE() && OP_TRUE() ) HIT(); testEqual( OPS, "F", beet$ "false && true" ); 43 | OPS = ""; if( OP_FALSE() && OP_FALSE() ) HIT(); testEqual( OPS, "F", beet$ "false && false" ); 44 | OPS = ""; if( OP_TRUE() && OP_TRUE() ) HIT(); testEqual( OPS, "TTH", beet$ "true && true" ); 45 | OPS = ""; if( OP_TRUE() && OP_FALSE() ) HIT(); testEqual( OPS, "TF", beet$ "true && false" ); 46 | 47 | OPS = ""; if( OP_FALSE() || OP_TRUE() ) HIT(); testEqual( OPS, "FTH", beet$ "false || true" ); 48 | OPS = ""; if( OP_FALSE() || OP_FALSE() ) HIT(); testEqual( OPS, "FF", beet$ "false || false" ); 49 | OPS = ""; if( OP_TRUE() || OP_TRUE() ) HIT(); testEqual( OPS, "TH", beet$ "true || true" ); 50 | OPS = ""; if( OP_TRUE() || OP_FALSE() ) HIT(); testEqual( OPS, "TH", beet$ "true || false" ); 51 | 52 | 53 | print( "\n\tboolean tests with 3 operands\n" ); 54 | 55 | OPS = ""; if( OP_TRUE() && OP_FALSE() && OP_FALSE() ) HIT(); testEqual( OPS, "TF", beet$ "T&F&F" ); 56 | OPS = ""; if( OP_TRUE() && OP_TRUE() && OP_FALSE() ) HIT(); testEqual( OPS, "TTF", beet$ "T&T&F" ); 57 | OPS = ""; if( OP_TRUE() && OP_TRUE() || OP_FALSE() ) HIT(); testEqual( OPS, "TTH", beet$ "T&T|F" ); 58 | OPS = ""; if( OP_TRUE() && OP_FALSE() || OP_TRUE() ) HIT(); testEqual( OPS, "TFTH", beet$ "T&F|T" ); 59 | OPS = ""; if( OP_FALSE() || OP_TRUE() && OP_FALSE() ) HIT(); testEqual( OPS, "FTF", beet$ "F|T&F" ); 60 | OPS = ""; if( OP_FALSE() || OP_TRUE() && OP_TRUE() ) HIT(); testEqual( OPS, "FTTH", beet$ "F|T&T" ); 61 | -------------------------------------------------------------------------------- /tests/175-vararg_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS = ""; 3 | 4 | global funcparams = 5 | { 6 | "0" = "", 7 | "1" = "a", 8 | "2" = "a,b", 9 | "3" = "a,b,c", 10 | }; 11 | global funcargs = 12 | { 13 | "0" = "", 14 | "1" = "1", 15 | "2" = "1,2", 16 | "3" = "1,2,3", 17 | }; 18 | global argretrieval = 19 | { 20 | "va_get_args" = "va_get_args()", 21 | "va_get_arg / va_arg_count" = "[va_get_arg(0),va_get_arg(1),va_get_arg(2),va_get_arg(3)].resize(va_arg_count())", 22 | }; 23 | global givethis = 24 | { 25 | "" = ["", "null"], 26 | "t" = ["0!", "0"], 27 | }; 28 | global getthis = 29 | { 30 | "" = "", 31 | "t" = "this .. ", 32 | }; 33 | global thismap = 34 | { 35 | "null" = "", 36 | "0" = "", 37 | "nullt" = givethis[""][1], 38 | "0t" = givethis["t"][1], 39 | }; 40 | global firstcall = 41 | { 42 | "following call" = false, // called from SGS 43 | "first call" = true, // called from C 44 | }; 45 | 46 | function generate_code() 47 | { 48 | out = "\n"; 49 | tc = 0; 50 | foreach( fc_name, isfirstcall : firstcall ) // whether called from C (first call) or SGS (following call) 51 | foreach( argret_name, argret_value : argretrieval ) // argument retrieval method 52 | foreach( getthis_name, getthis_value : getthis ) // is 'this' expected? 53 | foreach( givethis_name, givethis_value : givethis ) // is 'this' given? 54 | foreach( funcparam_count, funcparam_value : funcparams ) // expected arguments 55 | foreach( funcarg_count, funcarg_value : funcargs ) // given arguments 56 | { 57 | pthis = thismap[ givethis_value[1] .. getthis_name ]; 58 | funcdef = "function(" .. funcparam_value .. "){ testEqual( " 59 | .. getthis_value .. "tostring(" .. argret_value .. "), \"" .. pthis .. "[" .. funcarg_value 60 | .. "]\", \"EXP=" .. getthis_name .. funcparam_count 61 | .. " RECV=" .. givethis_name .. funcarg_count .. " " 62 | .. " | " .. argret_name .. " | " .. fc_name .. "\" ); }"; 63 | if( !isfirstcall ) 64 | { 65 | out ..= "(" .. givethis_value[0] .. funcdef .. "(" .. funcarg_value .. "));\n"; 66 | } 67 | else 68 | { 69 | out ..= "(" .. funcdef .. ".apply(" .. givethis_value[1] .. ", [" .. funcarg_value .. "]));\n"; 70 | } 71 | tc++; 72 | } 73 | out ..= "testEqual( tests_ran, " .. tc .. ", 'all " .. tc .. " tests have been triggered' );\n"; 74 | // println(out); 75 | return out; 76 | } 77 | 78 | eval( generate_code() ); 79 | 80 | -------------------------------------------------------------------------------- /tests/200-closure_MT.sgs: -------------------------------------------------------------------------------- 1 | 2 | // `closure` 3 | rec `` 4 | exec ` 5 | str1 = "banana"; 6 | dafuq = function( pre ) use( str1 ){ println( pre, str1 ); }; 7 | dafuq( "this is " ); 8 | 9 | str2 = "apple"; 10 | dafuq2 = function() use( str2 ){ println( this.text, str2 ); }; 11 | obj = { text = "that is ", func = dafuq2 }; 12 | obj.func(); 13 | ` 14 | result `SUCCESS` 15 | check_err `` 16 | check_out `this is banana 17 | that is apple 18 | ` 19 | 20 | // `closure 2` 21 | rec `` 22 | exec ` 23 | x = 5; 24 | 25 | y = function( a ) use( x ) 26 | { 27 | x += a; 28 | }; 29 | 30 | y( 4 ); 31 | print( x ); 32 | ` 33 | result `SUCCESS` 34 | check_err `` 35 | check_out `9` 36 | 37 | // `closure - pass through` 38 | rec `` 39 | exec ` 40 | x = 5; 41 | fn = function( a ) use( x ) 42 | { 43 | fn2 = function( a ) use(x) 44 | { 45 | x *= a; 46 | }; 47 | fn2( a ); 48 | x += a; 49 | }; 50 | fn( 4 ); 51 | print(x); 52 | ` 53 | result `SUCCESS` 54 | check_err `` 55 | check_out `24` 56 | 57 | // `closure - reference self (function)` 58 | rec `` 59 | exec ` 60 | function fn( a ) use(fn) 61 | { 62 | if( a >= 10 ) 63 | return a + 1; 64 | else 65 | return fn( a + 2 ); 66 | } 67 | print( fn(0) ); 68 | ` 69 | result `SUCCESS` 70 | check_err `` 71 | check_out `11` 72 | 73 | // `closure - reference global (and fail)` 74 | rec `` 75 | exec ` 76 | global x = 5; 77 | fn = function( a ) use( x ) 78 | { 79 | x += a; 80 | }; 81 | fn( 4 ); 82 | print(x); 83 | ` 84 | result `ECOMP` 85 | check_err `[E:[line 2] Variable storage redefined: closure -> global]` 86 | check_out `` 87 | 88 | // `closure + thiscall` 89 | rec `` 90 | exec ` 91 | a = 5; 92 | function fn1( x ) use( a ) 93 | { 94 | print( this, "|", x, "|", a, "|" ); 95 | } 96 | true!fn1( "arg1" ); 97 | fn1.apply( false, [ "arg2" ]); 98 | ` 99 | result `SUCCESS` 100 | check_err `` 101 | check_out `true|arg1|5|false|arg2|5|` 102 | 103 | // `extended closure test` 104 | rec `` 105 | exec ` 106 | q = function(b,x) 107 | { 108 | y = true; 109 | return function() use( x, y ) 110 | { 111 | return x; 112 | }; 113 | }; 114 | fn = q(true,5); 115 | printvar( fn ); 116 | println( fn() ); 117 | gc_collect(); 118 | q(true,6); 119 | printvar( fn ); 120 | println( fn() ); 121 | gc_collect(); 122 | ` 123 | result `SUCCESS` 124 | check_err `` 125 | check_out `closure 126 | { 127 | func: SGS function 128 | #0 (rc=1): int (5) 129 | #1 (rc=1): bool (true) 130 | } 131 | 5 132 | closure 133 | { 134 | func: SGS function 135 | #0 (rc=1): int (5) 136 | #1 (rc=1): bool (true) 137 | } 138 | 5 139 | ` 140 | 141 | // `serialization test` 142 | rec `` 143 | exec ` 144 | q = function(b,x) 145 | { 146 | y = true; 147 | return function() use( x, y ) 148 | { 149 | return x; 150 | }; 151 | }; 152 | global sq = serialize(q); 153 | global sqi = serialize(q(true,5));` 154 | result `SUCCESS` 155 | check_out `` 156 | check_err `` 157 | 158 | // `unserialization test` 159 | rec `` 160 | exec ` 161 | usq = unserialize(sq); 162 | usqi = unserialize(sqi); 163 | printvar(usqi()); 164 | printvar(usq(true,7));` 165 | result `SUCCESS` 166 | check_err `` 167 | check_out `int (5) 168 | closure 169 | { 170 | func: SGS function defined at :5 171 | #0 (rc=1): int (7) 172 | #1 (rc=1): bool (true) 173 | } 174 | ` 175 | 176 | // `self-ref. closure serialize` 177 | rec `` 178 | exec ` 179 | // do not define as 'function fn' statement, that creates a symbol 180 | fn = function( a ) use(fn) 181 | { 182 | if( a >= 10 ) 183 | return a + 1; 184 | else 185 | return fn( a + 2 ); 186 | }; 187 | println( fn(0) ); 188 | printvar_ext(fn,2); 189 | global sqsr = serialize(fn); 190 | ` 191 | result `SUCCESS` 192 | check_err `` 193 | check_out `11 194 | closure 195 | { 196 | func: SGS function 197 | #0 (rc=2): closure 198 | { 199 | func: ... 200 | #0 (rc=2): ... 201 | } 202 | } 203 | ` 204 | 205 | // `self-ref. closure unserialize` 206 | rec `` 207 | exec ` 208 | usqsr = unserialize(sqsr); 209 | println( usqsr(0) ); 210 | printvar_ext(usqsr,2); 211 | ` 212 | result `SUCCESS` 213 | check_err `` 214 | check_out `11 215 | closure 216 | { 217 | func: SGS function defined at :3 218 | #0 (rc=1): closure 219 | { 220 | func: ... 221 | #0 (rc=1): ... 222 | } 223 | } 224 | ` 225 | -------------------------------------------------------------------------------- /tests/330-metamethods_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS = ""; 3 | 4 | global called = {}; 5 | 6 | obj = {a=1,b=2}; 7 | objcp = {a=1,b=2}; 8 | obj2 = {a=10,b=20}; 9 | global iface = 10 | { 11 | __typeof = function(){ called.typeof = this; return "typeIFACE"; }, 12 | __clone = function(){ called.clone = this; return "cloneTest"; }, 13 | __tobool = function(){ called.tobool = this; return true; }, 14 | __tostring = function(){ called.tostring = this; return "IFACE"; }, 15 | __negate = function(){ called.negate = this; return -123456; }, 16 | __add = function(a,b){ called.add = this; return class({a=a.a+b.a,b=a.b+b.b},iface); }, 17 | __sub = function(a,b){ called.sub = this; return class({a=a.a-b.a,b=a.b-b.b},iface); }, 18 | __mul = function(a,b){ called.mul = this; return class({a=a.a*b.a,b=a.b*b.b},iface); }, 19 | __div = function(a,b){ called.div = this; return class({a=a.a/b.a,b=a.b/b.b},iface); }, 20 | __mod = function(a,b){ called.mod = this; return class({a=a.a%b.a,b=a.b%b.b},iface); }, 21 | __compare = function(a,b){ called.compare = this; return a.a <=> b.a || a.b <=> b.b; }, 22 | __call = function(self,a,b){ called.call = this; called.callself = self; return a,b; }, 23 | __getindex = function(k){ called.getindex = this; m = metaobj_get(this); 24 | if( isset( m, k ) ) return m[ k ]; 25 | return "[" $ k $ "]"; }, 26 | __setindex = function(k,v){ called.setindex = this; this[k] = v; this["_did_set_"$k] = true; }, 27 | }; 28 | class( obj, iface ); 29 | class( obj2, iface ); 30 | class( objcp, iface ); 31 | cntnr = { me = obj }; 32 | 33 | testEqual( metaobj_get(obj), iface, "metaobj - is set?" ); 34 | 35 | testEqual( typeof(obj), "typeIFACE", "metamethod - typeof - result" ); 36 | testEqual( @called.typeof, obj, "metamethod - typeof - correctly called" ); 37 | 38 | testEqual( clone(obj), "cloneTest", "metamethod - clone - result" ); 39 | testEqual( @called.clone, obj, "metamethod - clone - correctly called" ); 40 | 41 | testEqual( tobool(obj), true, "metamethod - tobool - result" ); 42 | testEqual( @called.tobool, obj, "metamethod - tobool - correctly called" ); 43 | 44 | testEqual( tostring(obj), "IFACE", "metamethod - tostring - result" ); 45 | testEqual( @called.tostring, obj, "metamethod - tostring - correctly called" ); 46 | 47 | testEqual( -obj, -123456, "metamethod - negate - result" ); 48 | testEqual( @called.negate, obj, "metamethod - negate - correctly called" ); 49 | 50 | testEqual( (obj+obj2).a, 11, "metamethod - add - result" ); 51 | testEqual( @called.add, obj, "metamethod - add - correctly called" ); 52 | 53 | testEqual( (obj-obj2).a, -9, "metamethod - sub - result" ); 54 | testEqual( @called.sub, obj, "metamethod - sub - correctly called" ); 55 | 56 | testEqual( (obj*obj2).a, 10, "metamethod - mul - result" ); 57 | testEqual( @called.mul, obj, "metamethod - mul - correctly called" ); 58 | 59 | testEqual( (obj/obj2).a, 0.1, "metamethod - div - result" ); 60 | testEqual( @called.div, obj, "metamethod - div - correctly called" ); 61 | 62 | testEqual( (obj%obj2).a, 1, "metamethod - mod - result" ); 63 | testEqual( @called.mod, obj, "metamethod - mod - correctly called" ); 64 | 65 | testEqual( obj==objcp, true, "metamethod - compare - result" ); 66 | testEqual( @called.compare, obj, "metamethod - compare - correctly called" ); 67 | 68 | testEqual( obj(5), 5, "metamethod - call/1 - result" ); 69 | testEqual( @called.call, obj, "metamethod - call/1 - correctly called/1" ); 70 | testEqual( @called.callself, null, "metamethod - call/1 - correctly called/2" ); 71 | 72 | testEqual( cntnr.me(5), 5, "metamethod - call/2 - result" ); 73 | testEqual( @called.call, obj, "metamethod - call/2 - correctly called/1" ); 74 | testEqual( @called.callself, cntnr, "metamethod - call/2 - correctly called/2" ); 75 | 76 | testEqual( obj.undef, "[undef]", "metamethod - getindex - result" ); 77 | testEqual( @called.getindex, obj, "metamethod - getindex - correctly called" ); 78 | 79 | obj.smth = 1; 80 | testEqual( obj.smth, 1, "metamethod - setindex - result" ); 81 | testEqual( obj._did_set_smth, true, "metamethod - setindex - side effect test" ); 82 | testEqual( @called.setindex, obj, "metamethod - setindex - correctly called" ); 83 | 84 | 85 | array.__getindex = function(k){ return "bacon"; }; 86 | testEqual( @array.undef, null, "metamethod - no getindex interference with arrays 1" ); 87 | testEqual( @[][1], null, "metamethod - no getindex interference with arrays 2" ); 88 | 89 | 90 | iface.__getindex = mm_getindex_router; 91 | iface.__get_testVar = function(){ called.get_var = this; return "success"; }; 92 | testEqual( obj.testVar, "success", "metamethod - getindex router" ); 93 | testEqual( called.get_var, obj, "metamethod - getindex router - correctly called" ); 94 | 95 | iface.__setindex = mm_setindex_router; 96 | iface.__set_testVar = function(v){ called.set_var = this; this.subTestVar = v; }; 97 | obj.testVar = 37; 98 | testEqual( obj.subTestVar, 37, "metamethod - setindex router" ); 99 | testEqual( called.set_var, obj, "metamethod - setindex router - correctly called" ); 100 | 101 | testEqual( ERRORS, "", "metamethod - no errors while accessing anything so far" ); 102 | -------------------------------------------------------------------------------- /tests/360-class_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS, tests_failed, tests_ran; 3 | 4 | // 5 | function section( x ){ print( "\n\t<< ", x, " >>\n" ); } 6 | print( "\n\n-- CLASS --\n" ); 7 | 8 | section( "class core" ); 9 | ERRORS = ""; 10 | class a {} 11 | testEqual( typeof(a), "dict", "class 'a' is defined" ); 12 | testEqual( _G.a, a, "... as a global" ); 13 | testEqual( sym_get("a"), a, "... as a symbol" ); 14 | testEqual( a.__name, "a", "... and has '__name' = 'a'" ); 15 | class b : a {} 16 | testEqual( typeof(b), "dict", "class 'b' is defined" ); 17 | testEqual( a.__name, "a", "'__name' = 'b'" ); 18 | testEqual( metaobj_get(b), a, "metaobj of 'b' is 'a'" ); 19 | testEqual( ERRORS, "", "class core - no errors" ); 20 | 21 | section( "class variables" ); 22 | ERRORS = ""; 23 | class c { global quel = 123; } 24 | testEqual( typeof(c), "dict", "class 'c' is defined" ); 25 | testEqual( c.quel, 123, "c.quel = 123" ); 26 | quel = "notglobal"; 27 | testEqual( @_G.quel, null, "accidentally did not make 'quel' global" ); 28 | class d { global fun = function(){ _G.yo = 2; return 1; }; } 29 | testEqual( d.fun(), 1, "successfully called d.fun (class global) [1]" ); 30 | testEqual( _G.yo, 2, "successfully called d.fun (class global) [2]" ); 31 | testEqual( ERRORS, "", "class variables - no errors" ); 32 | 33 | section( "class functions" ); 34 | ERRORS = ""; 35 | class e { global q = 5; function funct( a ){ return this.q + a; } } 36 | printvar(e); 37 | testEqual( typeof(e), "dict", "class 'e' is defined" ); 38 | testEqual( e.q, 5, "e.q = 5" ); 39 | testEqual( typeof(e.funct), "func", "function 'e.funct' is defined" ); 40 | testEqual( typeof(sym_get("e.funct")), "func", "symbol 'e.funct' is defined" ); 41 | testEqual( sym_get("e.funct"), e.funct, "symbol 'e.funct' is equal to function 'e.funct'" ); 42 | funct = "notglobal"; 43 | testEqual( @_G.funct, null, "accidentally did not make 'funct' global" ); 44 | testEqual( e.funct( 101 ), 106, "e.funct(101) = 106 / class function successfully called" ); 45 | inst_e = class( {}, e ); 46 | testEqual( inst_e.funct( 102 ), 107, "inst_e(102) = 107 / class method successfully called from instance" ); 47 | testEqual( ERRORS, "", "class functions - no errors" ); 48 | 49 | -------------------------------------------------------------------------------- /tests/500-misc_MT.sgs: -------------------------------------------------------------------------------- 1 | 2 | // `radial text gradient rendering` 3 | exec `include_library( "math" ); 4 | 5 | function lookup( dst ) 6 | { 7 | if( dst < 1 ) return "."; 8 | else if( dst < 2 ) return "o"; 9 | else if( dst < 3 ) return "O"; 10 | else if( dst < 4 ) return "#"; 11 | else if( dst < 5 ) return "O"; 12 | else if( dst < 6 ) return "o"; 13 | else return "."; 14 | } 15 | 16 | for( var y = -7; y <= 7; ++y ) 17 | { 18 | for( var x = -7; x <= 7; ++x ) 19 | { 20 | var dst = sqrt( x * x + y * y ); 21 | print( lookup( dst * 0.9 ) $ ' ' ); 22 | } 23 | print( "\n" ); 24 | }` 25 | result `SUCCESS` 26 | 27 | // `various syntax constructs` 28 | exec `include_library( "math" ); 29 | 30 | function test( x ) 31 | { 32 | var y; 33 | y = x; 34 | while( x ) 35 | { 36 | print( '#' ); 37 | x -= 1; 38 | } 39 | print( ' cos x = ', cos( y ), '\n' ); 40 | } 41 | var x, y; 42 | x = 10; 43 | while( x ) 44 | { 45 | test( x ); 46 | y += x -= 1; 47 | print( 'x = ', x, ', y = ', y, '\n' ); 48 | }` 49 | result `SUCCESS` 50 | 51 | // `dictionary stress test` 52 | rec `` 53 | exec `var obj = {}; 54 | println( "filling up..." ); 55 | for( var i = 0; i < 500; ++i ) 56 | obj[ i ] = ftime(); 57 | for( var i = 0; i < 500; ++i ) 58 | dummy = obj[ i ]; 59 | println( dict_size( obj ) ); 60 | for( var i = 0; i < 500; ++i ) 61 | unset( obj, i ); 62 | println( obj, " unloaded!" );` 63 | result `SUCCESS` 64 | check_err `` 65 | check_out `filling up... 66 | 500 67 | {} unloaded! 68 | ` 69 | 70 | // `error stack frame (check log, TODO test it)` 71 | exec ` 72 | include_library( "string" ); 73 | 74 | function 75 | blah 76 | ( 77 | ) 78 | { 79 | print 80 | ( 81 | string_cut 82 | ( 83 | null 84 | , 85 | 0 86 | ) 87 | ) 88 | ; 89 | } 90 | 91 | function 92 | foo 93 | ( 94 | ) 95 | { 96 | blah 97 | ( 98 | ) 99 | ; 100 | } 101 | 102 | 103 | foo 104 | ( 105 | ) 106 | ; 107 | ` 108 | result `SUCCESS` 109 | 110 | // `error stack frame 2` 111 | exec ` 112 | include_library( "string" ); 113 | 114 | var iface = 115 | { 116 | __add = function( a, b ){ string_cut( null, 0 ); } 117 | }; 118 | var whoa = class( {}, iface ); 119 | print( whoa + whoa ); 120 | ` 121 | result `SUCCESS` 122 | -------------------------------------------------------------------------------- /tests/530-dmg_ctrl_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS; 3 | global tests_failed, tests_ran; 4 | 5 | // 6 | function section( x ){ print( "\n\t<< ", x, " >>\n" ); } 7 | print( "\n\n-- DAMAGE RESILIENCE TESTING --\n" ); 8 | // 9 | 10 | section( "non-callables" ); 11 | ERRORS = ""; 12 | apply = section.apply; 13 | testEqual( ERRORS, "", "'apply' retrieval - no errors" ); 14 | 15 | ERRORS = ""; 16 | null!apply(); 17 | testEqual( ERRORS, "[W:apply(): 'this' - expected callable, got null]", "using 'apply' with 'null' - errors" ); 18 | 19 | ERRORS = ""; 20 | iface = {}; 21 | inst = class( {}, iface ); 22 | iface.__getindex = mm_getindex_router; 23 | iface.__setindex = mm_setindex_router; 24 | iface.__get_testVar = null; 25 | iface.__set_testVar = null; 26 | testEqual( inst.testVar, null, "using mm_getindex_router with null getter - ret.val." ); 27 | testEqual( ERRORS, "[E:mm_getindex_router(): Variable of type 'null' cannot be called]", "using mm_getindex_router with null getter - error" ); 28 | ERRORS = ""; 29 | testEqual( inst.testVar2, null, "using mm_getindex_router with nonexistent getter - ret.val." ); 30 | testEqual( ERRORS, "[W:Readable property not found: \"testVar2\"]", "using mm_getindex_router with nonexistent getter - error" ); 31 | ERRORS = ""; 32 | inst.testVar = 1; 33 | testEqual( ERRORS, "[E:mm_setindex_router(): Variable of type 'null' cannot be called]", "using mm_setindex_router with null setter - error" ); 34 | 35 | ERRORS = ""; 36 | thread_create(null); 37 | testEqual( ERRORS, "[W:thread_create(): argument 1 - expected callable, got null]", "thread_create with null function" ); 38 | ERRORS = ""; 39 | subthread_create(null); 40 | testEqual( ERRORS, "[W:subthread_create(): argument 1 - expected callable, got null]", "subthread_create with null function" ); 41 | 42 | -------------------------------------------------------------------------------- /tests/650-type_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS; 3 | global tests_failed, tests_ran; 4 | 5 | // 6 | function section( x ){ print( "\n\t<< ", x, " >>\n" ); } 7 | 8 | 9 | // 10 | print( "\n\n-- TYPES --\n" ); 11 | // 12 | 13 | var a = "random string"; 14 | var b = "WHAT"; 15 | var c = "banana"; 16 | var d = " \t\r\n \t\r\n spacy \t\r\n \t\r\n "; 17 | 18 | 19 | var _N = null, _B = true, _I = 1, _R = 1.0, _S = "1", _F = section, _C = print, _O = [], _O2 = {}, _O3 = map(); 20 | 21 | section( "is_* - generic" ); 22 | testEqual( !is_numeric( _N ) && is_numeric( _B ) && is_numeric( _I ) && is_numeric( _R ) && 23 | !is_numeric( _F ) && !is_numeric( _C ) && !is_numeric( _O ), true, "is_numeric - basic tests" ); 24 | testEqual( is_numeric( _S ), true, "is_numeric - numeric string" ); 25 | testEqual( is_numeric( c ), false, "is_numeric - non-numeric string" ); 26 | testEqual( is_numeric( "1E+2" ) && is_numeric( "-123.456" ) && is_numeric( "0x7f" ) && 27 | is_numeric( "0b101" ) && is_numeric( "01234" ) && is_numeric( "0o427" ), true, "is_numeric - extra numeric formats" ); 28 | testEqual( !is_callable( _N ) && !is_callable( _B ) && !is_callable( _I ) && !is_callable( _R ) && 29 | !is_callable( _S ) && is_callable( _F ) && is_callable( _C ), true, "is_callable - basic tests" ); 30 | testEqual( is_callable( _O ), false, "is_callable - non-callable object" ); 31 | testEqual( is_callable( function()use(a){} ), true, "is_callable - callable object" ); 32 | 33 | section( "is_* - specific" ); 34 | testEqual( !is_array( _N ) && !is_array( _B ) && !is_array( _I ) && !is_array( _R ) && !is_array( _S ) && 35 | !is_array( _F ) && !is_array( _C ) && is_array( _O ) && !is_array( _O2 ) && !is_array( _O3 ), true, "is_array" ); 36 | testEqual( !is_dict( _N ) && !is_dict( _B ) && !is_dict( _I ) && !is_dict( _R ) && !is_dict( _S ) && 37 | !is_dict( _F ) && !is_dict( _C ) && !is_dict( _O ) && is_dict( _O2 ) && !is_dict( _O3 ), true, "is_dict" ); 38 | testEqual( !is_map( _N ) && !is_map( _B ) && !is_map( _I ) && !is_map( _R ) && !is_map( _S ) && 39 | !is_map( _F ) && !is_map( _C ) && !is_map( _O ) && !is_map( _O2 ) && is_map( _O3 ), true, "is_map" ); 40 | function is_iterable( x ){ return @get_iterator(x) !== null; } 41 | testEqual( !is_iterable( _N ) && !is_iterable( _B ) && !is_iterable( _I ) && !is_iterable( _R ) && !is_iterable( _S ) && 42 | !is_iterable( _F ) && !is_iterable( _C ) && is_iterable( _O ) && is_iterable( _O2 ) && is_iterable( _O3 ), true, "is_iterable" ); 43 | 44 | section( "type_*" ); 45 | testEqual( typeid( _N ) == VT_NULL && typeid( _B ) == VT_BOOL && typeid( _I ) == VT_INT && 46 | typeid( _R ) == VT_REAL && typeid( _S ) == VT_STRING && typeid( _F ) == VT_FUNC && 47 | typeid( _C ) == VT_CFUNC && typeid( _O ) == VT_OBJECT, true, "typeid - basic tests" ); 48 | 49 | section( "typeptr*" ); 50 | testEqual( typeptr( _N ) || typeptr( _B ) || typeptr( _I ) || typeptr( _R ) || typeptr( _S ) || 51 | typeptr( _F ) || typeptr( _C ), toptr(0), "typeptr - basic types" ); 52 | testEqual( typeptr( _O ), typeptr_by_name( "array" ), "typeptr/_by_name - array" ); 53 | testEqual( typeptr( _O2 ), typeptr_by_name( "dict" ), "typeptr/_by_name - dict" ); 54 | testEqual( typeptr( _O3 ), typeptr_by_name( "map" ), "typeptr/_by_name - map" ); 55 | 56 | section( "get_props" ); 57 | testEqual( tostring(get_props(_N)), "[]", "get_props - null" ); 58 | testEqual( tostring(get_props(_B)), "[]", "get_props - bool" ); 59 | testEqual( tostring(get_props(_I)), "[]", "get_props - int" ); 60 | testEqual( tostring(get_props(_R)), "[]", "get_props - real" ); 61 | testEqual( tostring(get_props(_S)), "[length]", "get_props - string" ); 62 | testEqual( tostring(get_props(_F)), "[]", "get_props - SGS function" ); 63 | testEqual( tostring(get_props(_C)), "[]", "get_props - C function" ); 64 | testEqual( tostring(get_props(_O)), "[size,capacity,first,last]", "get_props - object (array)" ); 65 | testEqual( tostring(get_props(_O2)), "[]", "get_props - object 2 (dict)" ); 66 | testEqual( tostring(get_props(_O3)), "[]", "get_props - object 3 (map)" ); 67 | 68 | 69 | // 70 | print( "\n\nTesting finished!\nRan ", tests_ran, " tests of which ", tests_failed, " failed.\n" ); 71 | // 72 | -------------------------------------------------------------------------------- /tests/675-sgson_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | include "string"; 3 | 4 | global ERRORS = ""; 5 | 6 | function tostr( x ) 7 | { 8 | if( typeof( x ) == "array" ) 9 | { 10 | out = ""; 11 | foreach( v : x ) 12 | { 13 | if( out != "" ) out $= ","; 14 | out $= tostr( v ); 15 | } 16 | return "[" $ out $ "]"; 17 | } 18 | else if( typeof( x ) == "dict" ) 19 | { 20 | out = ""; 21 | foreach( k, v : x ) 22 | { 23 | if( out != "" ) out $= ","; 24 | out $= k $ "=" $ tostr( v ); 25 | } 26 | return "{" $ out $ "}"; 27 | } 28 | else 29 | return tostring(x); 30 | } 31 | 32 | 33 | tests = 34 | { 35 | "" = "null", 36 | "[" = "null", 37 | "][" = "null", 38 | "[]" = "[]", 39 | "null" = "null", 40 | "[null]" = "[null]", 41 | "[[null]" = "null", 42 | "[[null]]" = "[[null]]", 43 | "[null null]" = "null", 44 | "[null,null]" = "[null,null]", 45 | "[null,null,]" = "[null,null]", 46 | "[null,,null]" = "null", 47 | "[,null,null]" = "null", 48 | "[[],[]]" = "[[],[]]", 49 | '"wat"' = "wat", 50 | '["huh","wat"]' = "[huh,wat]", 51 | "{}" = "{}", 52 | "{ }" = "{}", 53 | "ident" = "null", 54 | "{a=b}" = "null", 55 | '{"key"}' = "null", 56 | '{"key"=true}' = "{key=true}", 57 | '{"a"=false,"b"=true}' = "{a=false,b=true}", 58 | '{a=false,"b"=true,}' = "{a=false,b=true}", 59 | '{var=5}' = "{var=5}", // eval/keyword test 60 | '{"a"=false,,"b"=true}' = "null", 61 | '{,"a"=false,"b"=true}' = "null", 62 | '["a"}' = "null", 63 | '{"a"]' = "null", 64 | '{"a"=["b"]}' = "{a=[b]}", 65 | '[{"a"="b"}]' = "[{a=b}]", 66 | 'map{}' = "[map]{}", 67 | 'flap{}' = "null", 68 | 'map { }' = "[map]{}", 69 | 'map{a=1}' = "[map]{a=1}", 70 | '"\\n\\t\\r\\x7f"' = "\n\t\r\x7f", 71 | '"\\x"' = "null", 72 | '"\\xxx"' = "null", 73 | '"\\u123"' = "null", 74 | '{\n\t"a"= true\n}' = "{a=true}", 75 | '[true][false]' = "null", 76 | "{true=false}" = "null", 77 | "5" = "5", 78 | "45" = "45", 79 | "12.345" = "12.345", 80 | "0.234" = "0.234", 81 | "00" = "0", 82 | "01.23" = "1.23", 83 | "1.23e+5" = "123000", 84 | "1.23e-2" = "0.0123", 85 | '"\\x65"' = "e", 86 | }; 87 | 88 | foreach( test, expect : tests ) 89 | { 90 | ERRORS = ""; 91 | out = sgson_decode( test ); 92 | altout = eval( "return " $ test $ ";" ); 93 | str = tostr( out ); 94 | altstr = tostr( altout ); 95 | testEqual( str, expect, "decode success - " $ test ); 96 | if( expect != "null" || test == "null" ) 97 | { 98 | // eval may work where SGSON doesn't, we don't care about those cases 99 | testEqual( altstr, expect, "eval decode success - " $ test ); 100 | testEqual( ERRORS, "", "expecting no errors - " $ test ); 101 | back = sgson_encode( out ); 102 | testEqual( ERRORS, "", "reencode - expecting no errors - " $ test ); 103 | } 104 | } 105 | 106 | 107 | println( "--- encoding ---" ); 108 | 109 | enctests = 110 | { 111 | "null" = null, 112 | "[]" = [], 113 | "{}" = {}, 114 | '{a="b"}' = { a = "b" }, 115 | '{a=[]}' = { a = [] }, 116 | '{"a/b"=[]}' = { "a/b" = [] }, 117 | '{"a b"=1}' = { "a b" = 1 }, 118 | '{"0a"=1}' = { "0a" = 1 }, 119 | 'map{a="b"}' = map{ a = "b" }, 120 | 'map{a=[]}' = map{ a = [] }, 121 | 'map{"a/b"=[]}' = map{ "a/b" = [] }, 122 | 'map{"a b"=1}' = map{ "a b" = 1 }, 123 | 'map{"0a"=1}' = map{ "0a" = 1 }, 124 | 'map{[true]=1}' = map{ [true] = 1 }, 125 | 'map{[1]=1}' = map{ [1] = 1 }, 126 | 'map{[1.5]=1}' = map{ [1.5] = 1 }, 127 | 'map{[map{}]=1}' = map{ [map{}] = 1 }, 128 | 'map{[[]]=1}' = map{ [[]] = 1 }, 129 | "\"\\n\\r\\t\\x19\\x7F\"" = "\n\r\t\x19\x7F", 130 | 'sym_get("gc_collect")' = gc_collect, 131 | 'event(true)' = event(true), 132 | '[event(true)]' = [event(true)], 133 | '[event(true),event(false)]' = [event(true),event(false)], 134 | '{a=event(true)}' = {a=event(true)}, 135 | }; 136 | 137 | ERRORS = ""; 138 | foreach( expect, test : enctests ) 139 | { 140 | out = sgson_encode( test ); 141 | testEqual( out, expect ); 142 | back = sgson_decode( out ); 143 | testEqual( tostr( back ), tostr( test ), "redecode - " $ expect ); 144 | } 145 | testEqual( ERRORS, "", "encoding - no errors" ); 146 | 147 | 148 | println( "--- formatting ---" ); 149 | 150 | fmttests = 151 | { 152 | "null" = null, 153 | "[]" = [], 154 | '{ 155 | a = "b" 156 | }' 157 | = { a = "b" }, 158 | '{ 159 | a = [] 160 | }' 161 | = { a = [] }, 162 | '{ 163 | a = [ 164 | "b", 165 | "c" 166 | ] 167 | }' 168 | = { a = [ "b", "c" ] }, 169 | '{ 170 | entities = [ 171 | { 172 | x = 5, 173 | y = 6, 174 | visible = true 175 | }, 176 | { 177 | x = 7, 178 | y = 8.9, 179 | visible = false 180 | } 181 | ] 182 | }' 183 | = { entities = [ { x = 5, y = 6, visible = true }, { x = 7, y = 8.9, visible = false } ] } 184 | }; 185 | 186 | foreach( expect, test : fmttests ) 187 | { 188 | expect = string_replace( expect, ["\r\n", "\r"], "\n" ); 189 | out = sgson_encode( test, "\t" ); 190 | testEqual( out, expect ); 191 | back = sgson_decode( out ); 192 | testEqual( tostr( back ), tostr( test ), "^^^ redecode" ); 193 | } 194 | 195 | -------------------------------------------------------------------------------- /tests/730-fmt_TF.sgs: -------------------------------------------------------------------------------- 1 | /* SOME DATA 15 TO READ 26 BY THE PARSER 43 */ 2 | global ERRORS, tests_failed, tests_ran; 3 | 4 | // 5 | function section( x ){ print( "\n\t<< ", x, " >>\n" ); } 6 | print( "\n\n-- FMT FUNCTIONS --\n" ); 7 | include "io", "fmt"; 8 | // 9 | 10 | 11 | section( "fmt_pack*" ); 12 | testEqual( fmt_pack( "3s4c", "numbers", 51, 52, 53, 54 ), "num\x33\x34\x35\x36", "fmt_pack - basic string/char encoding" ); 13 | testEqual( ''$fmt_unpack("3s4c",fmt_pack("3s4c","numbers",51,52,53,54)), "[num,51,52,53,54]", "(un)pack roundtrip - 3s4c" ); 14 | testEqual( ''$fmt_unpack("wfxd",fmt_pack("wfxd",1024,13.37,13.37)), "[1024,13.37,13.37]", "(un)pack roundtrip - wfxd" ); 15 | 16 | section( "fmt_base64*" ); 17 | testEqual( fmt_base64_encode( "Man" ), "TWFu", "base64 encode - 'Man'=>'TWFu'" ); 18 | testEqual( fmt_base64_decode( "TWFu" ), "Man", "base64 decode - 'TWFu'=>'Man'" ); 19 | testEqual( fmt_base64_decode( fmt_base64_encode( "the red fo" ) ), "the red fo", "base64 roundtrip - 10 chars" ); 20 | testEqual( fmt_base64_decode( fmt_base64_encode( "the red fox" ) ), "the red fox", "base64 roundtrip - 11 chars" ); 21 | testEqual( fmt_base64_decode( fmt_base64_encode( "the red foxx" ) ), "the red foxx", "base64 roundtrip - 12 chars" ); 22 | 23 | section( "fmt_custom_encode" ); 24 | ERRORS = ""; 25 | testEqual( fmt_custom_encode( "abcdef", "abc", ":", FMT_NUMBER_HEX ), ":61:62:63def", "custom enc - basic test" ); 26 | // escape all unprintable characters and quote (") as ""\xHH"" 27 | testEqual( fmt_custom_encode( 'a"b c\nd', "^ !#-~", '""\\x', FMT_NUMBER_HEX, 2, '""' ), """a""\x22""b c""\x0a""d""", "custom enc - C encoding" ); 28 | testEqual( ERRORS, "", "- no errors" ); 29 | 30 | section( "fmt_file_parser" ); 31 | f = io_file( "tests/730-fmt_TF.sgs", FILE_READ ); 32 | testEqual( typeof(f), "file", "file was loaded?" ); 33 | fp = fmt_file_parser( f ); 34 | testEqual( typeof(fp), "fmtstream", "file-reading input stream was created?" ); 35 | testEqual( fp.check( "QQQ" ), false, "check - fail" ); 36 | testEqual( fp.check( "/* SOME DATA 15" ), true, "check - success" ); 37 | testEqual( fp.read( 11 ), " TO READ 26", "read 11 more bytes" ); 38 | testEqual( fp.read( 20 ), " BY THE PARSER 43 */", "read 20 more bytes" ); 39 | 40 | section( "fmt_string_parser / number parsing" ); 41 | fp = fmt_string_parser( " 17, 5.3, -124.1412, 12.3e+2 " 42 | $" -2358, 0x012834aef, 0o371324, 0b0101010110110101100011 " 43 | $" 010101001, 1010101010011, 10100101000, 0101010101010101001010101010101010010101010100101 " 44 | $" 0777, 122124671, 1263712, 131674145121 " 45 | $" -18792, +89014214, 09812647182, 128941+ " 46 | $" 9024acbdef, 1de4bacf81924, fff, abad1dea " ); 47 | fp.skipcc(", "); testEqual( fp.read_real(), 17.0, "real 1" ); 48 | fp.skipcc(", "); testEqual( fp.read_real(), 5.3, "real 2" ); 49 | fp.skipcc(", "); testEqual( fp.read_real(), -124.1412, "real 3" ); 50 | fp.skipcc(", "); testEqual( fp.read_real(), 12.3e+2, "real 4" ); 51 | fp.skipcc(", "); testEqual( fp.read_int(), -2358, "int 1" ); 52 | fp.skipcc(", "); testEqual( fp.read_int(), 0x012834aef, "int 2" ); 53 | fp.skipcc(", "); testEqual( fp.read_int(), 0o371324, "int 3" ); 54 | fp.skipcc(", "); testEqual( fp.read_int(), 0b0101010110110101100011, "int 4" ); 55 | fp.skipcc(", "); testEqual( fp.read_binary_int(), 0b010101001, "binary_int 1" ); 56 | fp.skipcc(", "); testEqual( fp.read_binary_int(), 0b1010101010011, "binary_int 2" ); 57 | fp.skipcc(", "); testEqual( fp.read_binary_int(), 0b10100101000, "binary_int 3" ); 58 | fp.skipcc(", "); testEqual( fp.read_binary_int(), 0b0101010101010101001010101010101010010101010100101, "binary_int 4" ); 59 | fp.skipcc(", "); testEqual( fp.read_octal_int(), 0o0777, "octal_int 1" ); 60 | fp.skipcc(", "); testEqual( fp.read_octal_int(), 0o122124671, "octal_int 2" ); 61 | fp.skipcc(", "); testEqual( fp.read_octal_int(), 0o1263712, "octal_int 3" ); 62 | fp.skipcc(", "); testEqual( fp.read_octal_int(), 0o131674145121, "octal_int 4" ); 63 | fp.skipcc(", "); testEqual( fp.read_decimal_int(), -18792, "decimal_int 1" ); 64 | fp.skipcc(", "); testEqual( fp.read_decimal_int(), +89014214, "decimal_int 2" ); 65 | fp.skipcc(", "); testEqual( fp.read_decimal_int(), 09812647182, "decimal_int 3" ); 66 | fp.skipcc(", "); testEqual( fp.read_decimal_int(), 128941, "decimal_int 4" ); 67 | fp.skipcc(", "); testEqual( fp.read_hex_int(), 0x9024acbdef, "hex_int 1" ); 68 | fp.skipcc(", "); testEqual( fp.read_hex_int(), 0x1de4bacf81924, "hex_int 2" ); 69 | fp.skipcc(", "); testEqual( fp.read_hex_int(), 0xfff, "hex_int 3" ); 70 | fp.skipcc(", "); testEqual( fp.read_hex_int(), 0xabad1dea, "hex_int 4" ); 71 | -------------------------------------------------------------------------------- /tests/760-io_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS; 3 | global tests_failed, tests_ran; 4 | 5 | // 6 | function section( x ){ print( "\n\t<< ", x, " >>\n" ); } 7 | print( "\n\n-- I/O FUNCTIONS --\n" ); 8 | include_library( "string" ); 9 | include_library( "io" ); 10 | // 11 | 12 | section( "io_setcwd/io_getcwd" ); 13 | testEqual( io_setcwd('tests'), true, "io_setcwd - basic directory change" ); 14 | testEqual( string_cut( io_getcwd(), -5, -1 ), 'tests', "io_getcwd - validation of prev. change" ); 15 | testEqual( io_setcwd('..'), true, "io_setcwd - reverting change" ); 16 | testEqual( io_setcwd('doesnotexist'), false, "io_setcwd - invalid directory change" ); 17 | 18 | section( "io_file_write/io_file_read" ); 19 | testEqual( io_file_write('tests/data/t.txt', 'four five six'), true, "io_file_write - basic write" ); 20 | testEqual( io_file_read('tests/data/t.txt'), 'four five six', "io_file_read - basic read" ); 21 | testEqual( io_file_read('tests/data/doesnotexist.txt'), null, "io_file_read - invalid read" ); 22 | 23 | section( "io_dir_create" ); 24 | testEqual( io_dir_create('tests/data/testdir'), true, "io_dir_create - basic creation" ); 25 | testEqual( io_dir_create('tests/data/doesnotexist/testdir'), false, "io_dir_create - impossible creation" ); 26 | 27 | section( "io_rename" ); 28 | testEqual( io_rename('tests/data/testdir','tests/data/td2'), true, "io_rename - basic renaming" ); 29 | testEqual( io_rename('tests/data/td2','tests/data/testdir'), true, "io_rename - basic renaming back" ); 30 | testEqual( io_rename('tests/data/doesnotexist','yeah'), false, "io_rename - invalid renaming" ); 31 | 32 | section( "io_file_exists/io_dir_exists" ); 33 | testEqual( io_file_exists('tests/data/t.txt'), true, "io_file_exists - valid path" ); 34 | testEqual( io_file_exists('tests/data/doesnotexist.txt'), false, "io_file_exists - invalid path" ); 35 | testEqual( io_dir_exists('tests/data/testdir'), true, "io_dir_exists - valid path" ); 36 | testEqual( io_dir_exists('tests/data/doesnotexist'), false, "io_dir_exists - invalid path" ); 37 | 38 | section( "io_stat" ); 39 | printvar( io_stat( "tests/data/t.txt" ) ); 40 | printvar( io_stat( "tests/data/testdir" ) ); 41 | testEqual( io_stat('tests/data/doesnotexist.txt'), null, "io_stat - invalid path" ); 42 | testEqual( io_stat('tests/data/doesnotexist'), null, "io_stat - invalid path" ); 43 | 44 | section( "io_dir_delete/io_file_delete" ); 45 | testEqual( io_file_delete('tests/data/t.txt'), true, "io_file_delete - valid file" ); 46 | testEqual( io_file_delete('tests/data/doesnotexist.txt'), false, "io_file_delete - invalid file" ); 47 | testEqual( io_dir_delete('tests/data/testdir'), true, "io_dir_delete - valid directory" ); 48 | testEqual( io_dir_delete('tests/data/doesnotexist'), false, "io_dir_delete - invalid directory" ); 49 | 50 | 51 | // 52 | print( "\n\nTesting finished!\nRan ", tests_ran, " tests of which ", tests_failed, " failed.\n" ); 53 | // 54 | -------------------------------------------------------------------------------- /tests/800-json_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS; 3 | ERRORS = ""; 4 | include "sgsjson"; 5 | 6 | testEqual( typeof(json_encode), "cfunc", "sgsjson library is loaded" ); 7 | testEqual( ERRORS, "", "sgsjson library loaded without errors" ); 8 | 9 | function tostr( x ) 10 | { 11 | if( typeof( x ) == "array" ) 12 | { 13 | out = ""; 14 | foreach( v : x ) 15 | { 16 | if( out != "" ) out $= ","; 17 | out $= tostr( v ); 18 | } 19 | return "[" $ out $ "]"; 20 | } 21 | else if( typeof( x ) == "dict" ) 22 | { 23 | out = ""; 24 | foreach( k, v : x ) 25 | { 26 | if( out != "" ) out $= ","; 27 | out $= k $ "=" $ tostr( v ); 28 | } 29 | return "{" $ out $ "}"; 30 | } 31 | else 32 | return tostring(x); 33 | } 34 | 35 | 36 | tests = 37 | { 38 | "" = "null", 39 | "[" = "null", 40 | "][" = "null", 41 | "[]" = "[]", 42 | "null" = "null", 43 | "[null]" = "[null]", 44 | "[[null]" = "null", 45 | "[[null]]" = "[[null]]", 46 | "[null null]" = "null", 47 | "[null,null]" = "[null,null]", 48 | "[null,null,]" = "null", 49 | "[null,,null]" = "null", 50 | "[,null,null]" = "null", 51 | "[[],[]]" = "[[],[]]", 52 | '"wat"' = "wat", 53 | '["huh","wat"]' = "[huh,wat]", 54 | "{}" = "{}", 55 | '{"key"}' = "null", 56 | '{"key":true}' = "{key=true}", 57 | '{"a":false,"b":true}' = "{a=false,b=true}", 58 | '{"a":false,"b":true,}' = "null", 59 | '{"a":false,,"b":true}' = "null", 60 | '{,"a":false,"b":true}' = "null", 61 | '["a"}' = "null", 62 | '{"a"]' = "null", 63 | '{"a":["b"]}' = "{a=[b]}", 64 | '[{"a":"b"}]' = "[{a=b}]", 65 | '"\\n\\t\\r\\f\\u2122"' = "\n\t\r\f\xe2\x84\xa2", 66 | '"\\x"' = "null", 67 | '"\\u123"' = "null", 68 | '{\n\t"a": true\n}' = "{a=true}", 69 | '[true][false]' = "null", 70 | "{true:false}" = "null", 71 | "5" = "5", 72 | "45" = "45", 73 | "12.345" = "12.345", 74 | "0.234" = "0.234", 75 | "00" = "null", 76 | "01.23" = "null", 77 | "1.23e5" = "123000", 78 | "1.23e-2" = "0.0123", 79 | '"\\u0065"' = "e", 80 | }; 81 | 82 | foreach( test, expect : tests ) 83 | { 84 | out = json_decode( test ); 85 | str = tostr( out ); 86 | testEqual( str, expect ); 87 | if( str !== expect ) 88 | { 89 | println( out ); 90 | printvar( out ); 91 | } 92 | } 93 | 94 | 95 | println( "--- encoding ---" ); 96 | 97 | enctests = 98 | { 99 | "null" = null, 100 | "[]" = [], 101 | "{}" = {}, 102 | '{"a":"b"}' = { a = "b" }, 103 | '{"a":[]}' = { a = [] }, 104 | }; 105 | 106 | foreach( expect, test : enctests ) 107 | { 108 | out = json_encode( test ); 109 | testEqual( out, expect ); 110 | } 111 | -------------------------------------------------------------------------------- /tests/830-meta_TF.sgs: -------------------------------------------------------------------------------- 1 | 2 | global ERRORS, CURFN; 3 | global tests_failed, tests_ran; 4 | 5 | ERRORS = ""; 6 | include "io", "math", "sgsmeta"; 7 | 8 | testEqual( typeof(meta_unpack), "cfunc", "meta library is loaded" ); 9 | testEqual( ERRORS, "", "meta library loaded without errors" ); 10 | 11 | // 12 | function section( x ){ print( "\n\t<< ", x, " >>\n" ); } 13 | 14 | function testCode( code ) 15 | { 16 | global ERRORS = ""; 17 | ccode = compile_sgs( code ); 18 | testEqual( typeof(ccode), "string", CURFN $ " - code compiled successfully" ); 19 | if( ccode === null ) 20 | println( ERRORS ); 21 | global ERRORS = ""; 22 | ucode = meta_unpack( ccode ); 23 | testEqual( typeof(ucode), "dict", CURFN $ " - unpacked successfully?" ); 24 | if( ucode === null ) 25 | println( ERRORS ); 26 | return ucode; 27 | } 28 | 29 | function testFunction( fn, name, line, gotthis, numargs ) 30 | { 31 | testEqual( fn.name, name, CURFN $ " - name ("$name$")" ); 32 | testEqual( fn.line, line, CURFN $ " - line num. ("$line$")" ); 33 | testEqual( fn.gotthis, gotthis, CURFN $ " - is a method? ("$gotthis$")" ); 34 | testEqual( fn.numargs, numargs, CURFN $ " - arg. count ("$numargs$")" ); 35 | } 36 | 37 | function testFindFunction( fn, name ) 38 | { 39 | foreach( const : fn.consts ) 40 | { 41 | if( const.type == VT_FUNC && const.data.name == name ) 42 | return const.data; 43 | } 44 | return null; 45 | } 46 | 47 | 48 | section( "meta_unpack" ); 49 | 50 | CURFN = "empty 'main'"; 51 | testFunction( testCode( "" ), "
", 0, false, 0 ); 52 | 53 | CURFN = "simple 'main'"; 54 | testFunction( testCode( "println(null,true,5,3.14,'wat');" ), "
", 0, false, 0 ); 55 | 56 | CURFN = "simple functions"; 57 | code = testCode( "function f1(a,b,c){}\nX = {};\nfunction X.f2(a){}\nfunction f3(a,b){print this,a,b;}" ); 58 | testFunction( code, "
", 0, false, 0 ); 59 | subfn = testFindFunction( code, "f1" ); 60 | testFunction( subfn, "f1", 1, false, 3 ); 61 | subfn = testFindFunction( code, "X.f2" ); 62 | testFunction( subfn, "X.f2", 3, false, 1 ); 63 | subfn = testFindFunction( code, "f3" ); 64 | testFunction( subfn, "f3", 4, true, 2 ); 65 | 66 | CURFN = "3-deep function"; 67 | code = testCode( "function f1(){\nfunction f2(){}}" ); 68 | subfn = testFindFunction( code, "f1" ); 69 | testFunction( subfn, "f1", 1, false, 0 ); 70 | subfn = testFindFunction( subfn, "f2" ); 71 | testFunction( subfn, "f2", 2, false, 0 ); 72 | 73 | 74 | section( "meta_tokens_parse" ); 75 | 76 | ERRORS = ""; 77 | tokens = meta_tokens_parse( io_file_read( "src/sgscript.h" ) ); 78 | testEqual( ERRORS, "", "parse C header - no errors" ); 79 | 80 | -------------------------------------------------------------------------------- /tests/900-perfreq_MT.sgs: -------------------------------------------------------------------------------- 1 | 2 | // `no allocs for basic operations` 3 | rec `` 4 | exec `allocs1 = sys_stat(4); 5 | a = 2+4*3/8-1%9; 6 | print(sys_stat(4) - allocs1); 7 | ` 8 | result `SUCCESS` 9 | check_err `` 10 | check_out `0` 11 | 12 | // `no allocs for plain function call` 13 | rec `` 14 | exec `function f(){} 15 | f(); // prealloc stack frame 16 | allocs1 = sys_stat(4); 17 | f(); 18 | print(sys_stat(4) - allocs1); 19 | ` 20 | result `SUCCESS` 21 | check_err `` 22 | check_out `0` 23 | 24 | // `no allocs for member call` 25 | rec `` 26 | exec `o = {}; 27 | function o.f(){} 28 | o.f(); // prealloc stack frame 29 | allocs1 = sys_stat(4); 30 | o.f(); 31 | print(sys_stat(4) - allocs1); 32 | ` 33 | result `SUCCESS` 34 | check_err `` 35 | check_out `0` 36 | 37 | // `one alloc for one string-string concat` 38 | rec `` 39 | exec `allocs1 = sys_stat(4); 40 | a = "71890237012" $ SGS_PATH; 41 | print(sys_stat(4) - allocs1); 42 | ` 43 | result `SUCCESS` 44 | check_err `` 45 | check_out `1` 46 | 47 | // `one alloc for string conversion` 48 | rec `` 49 | exec `allocs1 = sys_stat(4); 50 | tostring(5); 51 | print(sys_stat(4) - allocs1); 52 | ` 53 | result `SUCCESS` 54 | check_err `` 55 | check_out `1` 56 | 57 | // `no allocs for isset` 58 | rec `` 59 | exec `t = map{}; 60 | isset( _G, "a" ); // prealloc stack frame 61 | allocs1 = sys_stat(4); 62 | v1 = isset( t, "a" ); // string key 63 | v2 = isset( t, 5 ); // integer key 64 | println(sys_stat(4) - allocs1); 65 | println(v1); 66 | println(v2); 67 | ` 68 | result `SUCCESS` 69 | check_err `` 70 | check_out `0 71 | false 72 | false 73 | ` 74 | 75 | // `< 10 allocs for class definition` 76 | rec `` 77 | exec `global NEWCLASS = null; // ignore global table resize 78 | sym_register( "NEWCLASS", [] ); // ignore symbol table resize 79 | allocs1 = sys_stat(4); 80 | class NEWCLASS {} 81 | print(sys_stat(4) - allocs1 < 10); 82 | ` 83 | result `SUCCESS` 84 | check_err `` 85 | check_out `true` 86 | 87 | // `no allocs for class method call` 88 | rec `` 89 | exec `class NEWCLASS2 90 | { 91 | function method(){} 92 | } 93 | nci2 = new NEWCLASS2(); 94 | nci2.method(); // prealloc stack frame 95 | allocs1 = sys_stat(4); 96 | nci2.method(); 97 | print(sys_stat(4) - allocs1); 98 | ` 99 | result `SUCCESS` 100 | check_err `` 101 | check_out `0` 102 | 103 | // `no allocs for index metamethod routers` 104 | rec `` 105 | exec `class NEWCLASS3 106 | { 107 | function __get_item(){ global A = 1; } 108 | function __set_item(v){ global B = v; } 109 | } 110 | nci3 = new NEWCLASS3(); 111 | a = nci3.item; 112 | nci3.item = 2; 113 | allocs1 = sys_stat(4); 114 | a = nci3.item; 115 | nci3.item = 2; 116 | print(sys_stat(4) - allocs1,"/",A,"/",B); 117 | ` 118 | result `SUCCESS` 119 | check_err `` 120 | check_out `0/1/2` 121 | -------------------------------------------------------------------------------- /tests/data/output.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archo5/sgscript/980a513db16b63ce65bd84c6f5084e4af7376379/tests/data/output.dir -------------------------------------------------------------------------------- /tests/f_cutwhile.sgs: -------------------------------------------------------------------------------- 1 | { 2 | while( i > 0 ) 3 | } -------------------------------------------------------------------------------- /tests/monkey/monkey.sgs: -------------------------------------------------------------------------------- 1 | 2 | include_library( "string" ); 3 | include_library( "io" ); 4 | 5 | global tokens = io_file_read( "monkey_dict.txt" ); 6 | tokens = string_trim( string_replace( tokens, ["\n"," "," "," "," "], " " ) ); 7 | tokens = string_explode( tokens, " " ); 8 | 9 | function advance( cur ) 10 | { 11 | i = cur.size - 1; 12 | while( i >= 0 ) 13 | { 14 | cur[ i ]++; 15 | if( cur[ i ] >= tokens.size ) 16 | { 17 | cur[ i ] = 0; 18 | i--; 19 | } 20 | else 21 | break; 22 | } 23 | if( i == -1 ) 24 | cur.push( 0 ); 25 | } 26 | 27 | function produce( cur, sep ) 28 | { 29 | out = []; 30 | foreach( n : cur ) 31 | out.push( tokens[ n ] ); 32 | return string_implode( out, sep ); 33 | } 34 | 35 | cur = []; 36 | if( io_file_exists( "monkey_state.txt" ) ) 37 | { 38 | str = string_trim( io_file_read( "monkey_state.txt" ) ); 39 | cur = string_explode( str, ',' ); 40 | foreach( k,v : cur ) 41 | cur[k] = 0+v; 42 | } 43 | 44 | sys_replevel( SGS_ERROR + 1 ); 45 | 46 | ctr = 0; 47 | while( true ) 48 | { 49 | if( ctr++ % 1000 == 0 ) 50 | io_file_write( "monkey_state.txt", string_implode(cur,",")$"\n" ); 51 | advance( cur ); 52 | str = produce( cur, " " ); 53 | // println( "||| " $ str $ " |||" ); 54 | eval( str, true ); 55 | str = produce( cur, "" ); 56 | // println( "||| " $ str $ " |||" ); 57 | eval( str, true ); 58 | } 59 | -------------------------------------------------------------------------------- /tests/monkey/monkey_dict.txt: -------------------------------------------------------------------------------- 1 | { } ( ) [ ] ; : , . " ' 2 | + - * / % & | ^ && || $ << >> ! ~ 3 | += -= *= /= %= &= |= ^= &&= ||= $= <<= >>= 4 | < > <= >= == != === !== 5 | a b c aa bb cc 6 | if else do while for foreach 7 | return function break continue 8 | 0 1523, -8293, 0.2839, -43.289E+1 9 | 0x98ac3e0 0o16743 0b01011010101010100 10 | "what" '\this' 11 | -------------------------------------------------------------------------------- /tests/s_fnexp.sgs: -------------------------------------------------------------------------------- 1 | 2 | global sqr; 3 | sqr = function( x ) 4 | { 5 | return x*x; 6 | }; 7 | print( "sqr(5) = ", sqr(5) ); 8 | -------------------------------------------------------------------------------- /tests/s_for.sgs: -------------------------------------------------------------------------------- 1 | var i; 2 | for( i = 0; i < 5; ++i ) 3 | print( i ); --------------------------------------------------------------------------------