This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Exerro's profile picture

Nova scripting language

Started by Exerro, 12 April 2014 - 10:25 PM
Exerro #1
Posted 13 April 2014 - 12:25 AM
Hi,

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
Screenies:
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
More on classes:
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
Edited on 13 April 2014 - 02:41 PM
ardera #2
Posted 14 April 2014 - 08:59 PM
Looks nice! Awesome!
viluon #3
Posted 19 April 2014 - 02:21 PM
Well.. yeah. But isn't this useless? What are its advantages in comparison to standard Lua ?? I pretty much think u are reinventing the wheel…
ardera #4
Posted 19 April 2014 - 07:12 PM
Well.. yeah. But isn't this useless? What are its advantages in comparison to standard Lua ?? I pretty much think u are reinventing the wheel…
If you see it like that, Java would be a "reinvention of the wheel" too, because there was C before it.
One advantage is OOP, ie classes, methods, access declarations… (it's your thing if you say OOP is an advantage or not)
Ok this is a bit raw, but I'm sure it will be very useful when it's finished. Especially for larger Applications some classes would be nice.
Edited on 19 April 2014 - 05:12 PM
Yevano #5
Posted 19 April 2014 - 07:27 PM
You could improve this greatly by converting the code to tokens before the parsing stage, so that you aren't doing string matching while generating your AST (It's probably why your code looks so big and loopy). As well, you could store line and column numbers in the tokens so that you can give useful line number info with errors.
ElvishJerricco #6
Posted 19 April 2014 - 08:48 PM
Well.. yeah. But isn't this useless? What are its advantages in comparison to standard Lua ?? I pretty much think u are reinventing the wheel…

Lots of things are useless before they're useful. It's a baby. It might not survive to adulthood but for now it's good to keep alive.

Anyway take a look at some of the other language projects out there to get some ideas on how to improve this and fix some of the issues. For example, you're not building your AST quite right if you can't have function calls with several arguments as arguments to function calls, and there's a lot of projects you could look at to help figure it out. LuaLua is one you might be interested in, although it compels to bytecode.
Agoldfish #7
Posted 19 April 2014 - 09:52 PM

[shamelessplug]LuaLua is one you might be interested in, although it compels to bytecode.[/shamelessplug]
:P/>
Edited on 19 April 2014 - 07:52 PM
viluon #8
Posted 20 April 2014 - 03:07 PM
Well.. yeah. But isn't this useless? What are its advantages in comparison to standard Lua ?? I pretty much think u are reinventing the wheel…
If you see it like that, Java would be a "reinvention of the wheel" too, because there was C before it.
One advantage is OOP, ie classes, methods, access declarations… (it's your thing if you say OOP is an advantage or not)
Ok this is a bit raw, but I'm sure it will be very useful when it's finished. Especially for larger Applications some classes would be nice.

OOP is already possible (and easily implemented) without additional compilers.

http://www.lua.org/pil/16.html

And if I'd like an extension, I'd use http://www.computercraft.info/forums2/index.php?/topic/14550-see-standard-execution-environment/
ardera #9
Posted 20 April 2014 - 04:07 PM
OOP is already possible (and easily implemented) without additional compilers.

http://www.lua.org/pil/16.html

And if I'd like an extension, I'd use http://www.computerc...on-environment/
I knew someone would say that :P/>

There are so many OOP extensions for lua, but I don't like any. Lua is simply not good in this. Lua is meant to be used as a scripting language, and it's far better in scripting than using it for OOP purposes.

Look at "SEE" by Yevano: It's way to import stuff is exactly what I mean, it's done trough comments. In Java, it's done trough language statements because it's optimized for using imports and packages.
Nothing against SEE, sure it's good to have and if I'd have some big projects, I'd be using it, but this is better.



Suggestion:
Static Types?
(This would help debugging a lot, it would be better for foreseeing problems and bugs, and less runtime error's would occur.)