I just wrote a small scripting language, and although it is still in development, I thought I would upload it. Its current features are:
- Completely Lua free interpretter & value assignment
- If statements
- While loops
- Dynamic values
- Classes
code = output
code = output
To demonstrate how the language works, I will include a small example:
Spoiler
method sub( self, start, fin )
private str = ""; ~~ private = local
if ?self == "object" do ~~ ? is like type( ) in Lua
str = self.str; ~~ (I copied this from a string class which is why this bit is here
end
if ?str != "string" do ~~ not equal to
error( "expected string, got " + ?str ) ~~ use "string" + "string" to concat
end
start = ?start == "number" and start or 1; ~~ similar to lua and/or stuff
fin = ?fin == "number" and fin or #str;
private str2 = "";
private i = start;
while i <= fin do
str2 = str2 + ( str[i] or "" ); ~~ use string[n] to get individual chars
i++ ~~ add 1 to i
end
return str2; -- return the value
end
Notes:
- Only 1 value can be assigned per statement so you can't do "var1, var2 = unpack( { 1, 2 } )" for example.
- Function calls in variables cannot call other functions with multiple args because it will break the interpretter.
- print( x( "something" ), "something" ) is ok
- print( x( "something", "something" ), "something" ) is not ok
- The error handling system is bad to say the least. It doesn't return line numbers.
- Classes can have private methods, methods, variables, and static variables
- Tables can have numerical indexes assigned when created, but not anything else
- t = { 1, 2, 3 }; is ok
- t = { [1] = 1, [2] = 2, [3] = 3 }; is not ok
Spoiler
class myclass
static variable = 5; ~~ static means it can't be changed
number x = 1; ~~ it will only be changed if the value is a number
number string text = "hello"; ~~ can be number/string
method something( value )
private val = "\"" + tostring( value ) + "\"";
output( val )
end
private method init( ... )
private i = 1;
while i <= #arg do ~~ if ... is used in the args, it will be a table of args
output( arg[i] )
end
end
end
The current code is just under 2000 lines, and is lacking for loops and else/elseif statements, and I honestly probably won't be adding these in any time soon.
Anyway, I hope you can use this, or maybe this will inspire you to attempt making your own version / scripting language.
The String class I made is below:
Spoiler
class String
string str = "";
method sub( self, start, fin )
private str = self;
if ?self == "object" do
str = self.str;
end
if ?str != "string" do
error( "expected string, got " + ?str )
end
start = ?start == "number" and start or 1;
fin = ?fin == "number" and fin or #str;
private str2 = "";
private i = start;
while i <= fin do
str2 = str2 + ( str[i] or "" );
i++
end
return str2;
end
method repeat( self, count )
if ?count != "number" do
error( "expected number" )
end
private str = self;
if ?self == "object" do
str = self.str;
end
if ?str != "string" do
error( "expected string, got " + ?str )
end
private str2 = "";
private i = 1;
while i <= count do
str2 = str2 + str;
i++
end
return str2;
end
method insert( self, pat, start, clipping )
if ?pat != "string" do
error( "expected string, got " + ?pat )
end
if ?start != "number" do
error( "expected number, got " + ?start )
end
if ?clipping != "number" do
clipping = 0;
end
private str = self;
if ?self == "object" do
str = self.str;
end
private str2 = "";
private i = 1;
while i < start do
str2 = str2 + ( str[i] or "" );
i++
end
str2 = str2 + pat;
private i = start + clipping - 1;
while i < #str do
str2 = str2 + str[i];
i++
end
return str2;
end
private method init( self, str )
self.str = str;
end
end
str = new String( "Hello World!" ); ~~ objects of classes!
v = str.sub( 2, 3 );
output( v ) ~~ "el"
v = String.sub( "string", 2, 3 );
output( v ) ~~ "tr"
pastebin get eq2NP9xZ script