Jump to content

  • Log In with Google      Sign In   
  • Create Account






Scribe scripting library

Posted by Programmer16, in Scribe 28 February 2013 · 416 views

One of the projects I have always wanted to do is create a scripting language, but I have always failed horribly. My most recent attempt was kind of a mix of success and failure. The initial goals were to create a high level syntax (something similar to BASIC) and create a low level language (basically straight op codes; kind of like a simplified assembly.)
I meant to start out implementing the low level version first, but this didn't pan out how I wanted to and I ended up with a more BASIC-esque language. However it is working wonderfully, so I kept it and dubbed it Scribe 1.0. Currently it supports most of your basic features:
- Arithmetic operations
- Conditionals (if, elseif, else)
- Loops (loop, yield)
- Variables (booleans, strings, and numbers)
- Commands
- Labels

I achieved many of the goals that I wanted to, so I'm quite happy: it runs properly on both Windows and Android (and should hopefully , it runs decently (I haven't done any optimizations yet), it doesn't have any built in commands (so no sandboxing needed), and it is easily extendable (I am creating some packages that come with the library that provide some core functionality; they have to explicitly be registered though.)

I'm planning on eventually open-sourcing it, but I want to get it a little more stable and clean it up some first. Anyway, here is Tic Tac Toe written with Scribe and using the Console package:
// Initialize the board
clear_board:
@board11 = ' '
@board12 = ' '
@board13 = ' '
@board21 = ' '
@board22 = ' '
@board23 = ' '
@board31 = ' '
@board32 = ' '
@board33 = ' '
@playerToken = 'x'
@errorMessage = ''

game_loop:
Console.Clear
Console.SetTextColor Red
Console.WriteLine @errorMessage

Console.SetTextColor White
Console.WriteLine ' 1 2 3'
Console.WriteLine ' 1 ' @board11 | @board21 | @board31
Console.WriteLine ' -+-+-'
Console.WriteLine ' 2 ' @board12 | @board22 | @board32
Console.WriteLine ' -+-+-'
Console.WriteLine ' 3 ' @board13 | @board23 | @board33
Console.WriteLine ''

// Check win conditions
if @board11 != ' ' && @board11 == @board21 && @board11 == @board31
// Horizontal row 1
@winner = @board11
goto game_over
elseif @board12 != ' ' && @board12 == @board22 && @board12 == @board32
// Horizontal row 2
@winner = @board12
goto game_over
elseif @board13 != ' ' && @board13 == @board23 && @board13 == @board33
// Horizontal row 3
@winner = @board13
goto game_over
elseif @board11 != ' ' && @board11 == @board12 && @board11 == @board13
// Vertical column 1
@winner = @board11
goto game_over
elseif @board21 != ' ' && @board21 == @board22 && @board21 == @board23
// Vertical column 2
@winner = @board21
goto game_over
elseif @board31 != ' ' && @board31 == @board32 && @board31 == @board33
// Vertical column 3
@winner = @board31
goto game_over
elseif @board11 != ' ' && @board11 == @board22 && @board11 == @board33
// Diagonal top left to bottom right
@winner = @board11
goto game_over
elseif @board31 != ' ' && @board31 == @board22 && @board31 == @board13
// Diagonal top right to bottom left
@winner = @board31
goto game_over

if @board11 != ' ' && @board12 != ' ' && @board13 != ' ' && @board21 != ' ' && @board22 != ' ' && @board23 != ' ' && @board31 != ' ' && @board32 != ' ' && @board33 != ' '
@winner = ''
goto game_over

Console.WriteLine 'Current player: ' @playerToken
Console.Write 'Choose a column [1-3]: '
-- Console.ReadLine playerColumn
Console.ReadKeyChar playerColumn false
if @playerColumn != '1' && @playerColumn != '2' && @playerColumn != '3'
@errorMessage = 'You must enter a value of 1, 2, or 3.'
goto game_loop

Console.WriteLine ''
Console.Write 'Choose a row [1-3]: '
-- Console.ReadLine playerRow
Console.ReadKeyChar playerRow false

if @playerRow != '1' && @playerRow != '2' && @playerRow != '3'
@errorMessage = 'You must enter a value of 1, 2, or 3.'
goto game_loop

// Set player node
if @playerColumn == '1' && @playerRow == '1'
@board11 = @playerToken
elseif @playerColumn == '2' && @playerRow == '1'
@board21 = @playerToken
elseif @playerColumn == '3' && @playerRow == '1'
@board31 = @playerToken
elseif @playerColumn == '1' && @playerRow == '2'
@board12 = @playerToken
elseif @playerColumn == '2' && @playerRow == '2'
@board22 = @playerToken
elseif @playerColumn == '3' && @playerRow == '2'
@board32 = @playerToken
elseif @playerColumn == '1' && @playerRow == '3'
@board13 = @playerToken
elseif @playerColumn == '2' && @playerRow == '3'
@board23 = @playerToken
elseif @playerColumn == '3' && @playerRow == '3'
@board33 = @playerToken

// Change player
if @playerToken == x
@playerToken = o
else
@playerToken = x

@errorMessage = ''
goto game_loop

game_over:
if @winner == ''
Console.WriteLine 'The game is a tie!'
else
Console.WriteLine @winner ' has won the game!'

Console.Write 'Play again? [Y/N]: '
Console.ReadKeyChar playAgainChoice true
Console.WriteLine ''
if @playAgainChoice == 'y' || @playAgainChoice == 'Y'
goto clear_board
Now I'm off to implement a few more packages (arrays for one lol) and do a little optimization. I've attached the little bit of documentation I've written up in case anyone is interested.

[edit]
For the curious, here is the C# code to run the above:
 
using Scribe;
 
namespace TicTacToe
{
	class Program
	{
		static void Main(string[] args)
		{
			var virtualMachine = new VirtualMachine();
			Scribe.Packages.Console.Register(virtualMachine);
			var thread = virtualMachine.LoadFile(@"C:\Users\Public\tictactoe.scribe");
			thread.Start(virtualMachine);
			while(thread.IsRunning);
		}
	}
}
I also updated the game's code above so that it allows you to replay without the console closing.

[edit]
Apparently at some point I switched from loop to while. Makes sense seeing as how that is what is used in most languages, but I don't quite remember doing so. I'll have to update the document to reflect this.

[edit]
Here is another example game, Guess the Number. This one includes use of the new Array package:
 
// -------------------------------------------------------------------------
// An implementation of Guess the Number written on the Scribe scripting
// library.
// Written by Donny Beals - February 28th, 2013
// Copyright © Beals Software 2013
// -------------------------------------------------------------------------
 
initialize:
Array.Create numbersGuessed 10 true
@choiceMessage = ''
Random.NextInRange numberToGuess 1, 10
 
game_loop:
Console.Clear
 
@index = 0
Array.Count numberOfGuesses @numbersGuessed
 
Console.Write 'Numbers guessed: '
while @index < @numberOfGuesses
	Array.GetValue arrayValue @numbersGuessed @index
	Console.Write @arrayValue ' '
	@index += 1
Console.WriteLine ''
	
Console.WriteLine @choiceMessage
Console.Write 'Choose a number from 1 to 9: '
Console.ReadKeyChar playerChoice false
 
if @playerChoice != '1' && @playerChoice != '2' && @playerChoice != '3' && @playerChoice != '4' && @playerChoice != '5' && @playerChoice != '6' && @playerChoice != '7' && @playerChoice != '8' && @playerChoice != '9'
	@choiceMessage = 'You must enter a valid number from 1 to 9.'
	goto game_loop
 
if @playerChoice < @numberToGuess
	@choiceMessage = 'Your guess is too low.'
	Array.AddValue @numbersGuessed @playerChoice
elseif @playerChoice > @numberToGuess
	@choiceMessage = 'Your guess is too high.'
	Array.AddValue @numbersGuessed @playerChoice
else
	goto game_over
	
goto game_loop
 
game_over:
Console.WriteLine ''
Console.WriteLine 'You guessed the number (' @numberToGuess ')'
 
Console.Write 'Play again? [Y/N]: '
Console.ReadKeyChar playAgainChoice true
Console.WriteLine ''
if @playAgainChoice == 'y' || @playAgainChoice == 'Y'
	goto initialize

Attached Files






Awesome great work. I really like the syntax of your scripting language it is very clean. Reminds of the simplicity of assembly without the annoying bits. Thumbs up.

Awesome great work. I really like the syntax of your scripting language it is very clean. Reminds of the simplicity of assembly without the annoying bits. Thumbs up.

Thank you! That is pretty much what I was going for, so I'm glad it turned out properly. There are still quite a few major issues with the core mechanics that I need to address before it can really become viable for anything. I'm planning on using it going forward though, so hopefully I'll be able to round it out to something useful lol. The ultimate goal is an easy to use scripting language with simple threading support which compiles down to bare op-codes/expressions ala MonoGame's content system (which also means cross-platform support between Windows, Android, iOS, Mac, and Linux.)

 

Thanks again for the comment, I appreciate it!

PARTNERS