Scribe scripting library

posted in Beals Software
Published February 28, 2013
Advertisement
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 boardclear_board:@board11 = ' '@board12 = ' '@board13 = ' '@board21 = ' '@board22 = ' '@board23 = ' '@board31 = ' '@board32 = ' '@board33 = ' '@playerToken = 'x'@errorMessage = ''game_loop:Console.ClearConsole.SetTextColor RedConsole.WriteLine @errorMessageConsole.SetTextColor WhiteConsole.WriteLine ' 1 2 3'Console.WriteLine ' 1 ' @board11 | @board21 | @board31Console.WriteLine ' -+-+-'Console.WriteLine ' 2 ' @board12 | @board22 | @board32Console.WriteLine ' -+-+-'Console.WriteLine ' 3 ' @board13 | @board23 | @board33Console.WriteLine ''// Check win conditionsif @board11 != ' ' && @board11 == @board21 && @board11 == @board31// Horizontal row 1@winner = @board11goto game_overelseif @board12 != ' ' && @board12 == @board22 && @board12 == @board32// Horizontal row 2@winner = @board12goto game_overelseif @board13 != ' ' && @board13 == @board23 && @board13 == @board33// Horizontal row 3@winner = @board13goto game_overelseif @board11 != ' ' && @board11 == @board12 && @board11 == @board13// Vertical column 1@winner = @board11goto game_overelseif @board21 != ' ' && @board21 == @board22 && @board21 == @board23// Vertical column 2@winner = @board21goto game_overelseif @board31 != ' ' && @board31 == @board32 && @board31 == @board33// Vertical column 3@winner = @board31goto game_overelseif @board11 != ' ' && @board11 == @board22 && @board11 == @board33// Diagonal top left to bottom right@winner = @board11goto game_overelseif @board31 != ' ' && @board31 == @board22 && @board31 == @board13// Diagonal top right to bottom left@winner = @board31goto game_overif @board11 != ' ' && @board12 != ' ' && @board13 != ' ' && @board21 != ' ' && @board22 != ' ' && @board23 != ' ' && @board31 != ' ' && @board32 != ' ' && @board33 != ' '@winner = ''goto game_overConsole.WriteLine 'Current player: ' @playerTokenConsole.Write 'Choose a column [1-3]: '-- Console.ReadLine playerColumnConsole.ReadKeyChar playerColumn falseif @playerColumn != '1' && @playerColumn != '2' && @playerColumn != '3'@errorMessage = 'You must enter a value of 1, 2, or 3.'goto game_loopConsole.WriteLine ''Console.Write 'Choose a row [1-3]: '-- Console.ReadLine playerRowConsole.ReadKeyChar playerRow falseif @playerRow != '1' && @playerRow != '2' && @playerRow != '3'@errorMessage = 'You must enter a value of 1, 2, or 3.'goto game_loop// Set player nodeif @playerColumn == '1' && @playerRow == '1'@board11 = @playerTokenelseif @playerColumn == '2' && @playerRow == '1'@board21 = @playerTokenelseif @playerColumn == '3' && @playerRow == '1'@board31 = @playerTokenelseif @playerColumn == '1' && @playerRow == '2'@board12 = @playerTokenelseif @playerColumn == '2' && @playerRow == '2'@board22 = @playerTokenelseif @playerColumn == '3' && @playerRow == '2'@board32 = @playerTokenelseif @playerColumn == '1' && @playerRow == '3'@board13 = @playerTokenelseif @playerColumn == '2' && @playerRow == '3'@board23 = @playerTokenelseif @playerColumn == '3' && @playerRow == '3'@board33 = @playerToken// Change playerif @playerToken == x@playerToken = oelse@playerToken = x@errorMessage = ''goto game_loopgame_over:if @winner == ''Console.WriteLine 'The game is a tie!'elseConsole.WriteLine @winner ' has won the game!'Console.Write 'Play again? [Y/N]: 'Console.ReadKeyChar playAgainChoice trueConsole.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 (C) Beals Software 2013// ------------------------------------------------------------------------- initialize:Array.Create numbersGuessed 10 true@choiceMessage = ''Random.NextInRange numberToGuess 1, 10 game_loop:Console.Clear @index = 0Array.Count numberOfGuesses @numbersGuessed Console.Write 'Numbers guessed: 'while @index < @numberOfGuesses Array.GetValue arrayValue @numbersGuessed @index Console.Write @arrayValue ' ' @index += 1Console.WriteLine '' Console.WriteLine @choiceMessageConsole.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 @playerChoiceelseif @playerChoice > @numberToGuess @choiceMessage = 'Your guess is too high.' Array.AddValue @numbersGuessed @playerChoiceelse 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 trueConsole.WriteLine ''if @playAgainChoice == 'y' || @playAgainChoice == 'Y' goto initialize
0 likes 2 comments

Comments

blewisjr
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.
March 01, 2013 09:43 PM
Programmer16

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!

March 02, 2013 06:18 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement