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

Nextbasic

Started by immibis, 11 September 2013 - 04:49 AM
immibis #1
Posted 11 September 2013 - 06:49 AM
(correct capitalization is NextBASIC. Forum is stupid)

Pastebin code: XechUbLP

This is a simple BASIC dialect written in Lua for CC.
There are no loops or functions, only single-line IF and GOTO. This is intentional to keep the number of language features down. Arrays are a future possibility.

Commands do not have to be in uppercase. Variable names are case insensitive (XYZ and xyz are the same variable)
String variables have names ending in $, number variables do not.

Supported commands:
LET variable = value - sets a variable.
PRINT something - prints text or numbers. See the example program for examples of the various ways you can use PRINT.
INPUT variable - gets input from the user. Works for both number and string variables.
IF condition THEN command - runs command if condition is true. See example program for examples of how to use this.
[label] - a label that GOTO can go to.
GOTO [label] - jumps to a label.
CLS - clears the screen
REM anything - does nothing (this is a comment)
WAIT value SECONDS - waits a certain amount of time

SET REDSTONE SIGNAL side ON/OFF - sets a redstone signal output
SET REDSTONE SIGNAL side colour ON/OFF - sets a bundled redstone signal output
WAIT FOR REDSTONE SIGNAL side ON/OFF - waits for a redstone signal to be on or off
WAIT FOR REDSTONE SIGNAL side colour ON/OFF - waits for a bundled redstone signal to be on or off
WAIT FOR REDSTONE SIGNAL CHANGE - waits for any redstone input to change

INT(number) - equivalent to math.floor in Lua, rounds a number down to the nearest integer.
RND() - random number between 0 and 1
VAL(string) - converts a string to a number
STR$(number) - converts a number to a string
LEN(number) - the length of a string
LEFT$(string, number) - a certain number of characters from the start of a string
RIGHT$(string, number) - a certain number of characters from the end of a string
MID$(string, start, length) - a certain number of characters from the middle of a string. start is the position of the first character to get.
LOWER$(string) - converts a string to lowercase
UPPER$(string) - converts a string to uppercase
TRIM$(string) - removes spaces from the start and end of a string
INSTR(string to look in, string to look for, start position) - the position where one string occurs in another, or -1 if not found. Example: INSTR("hello world", "lo", 1) is 4. Always starts looking at start position.

< > <> <= >= = - comparison operators (respectively: greater than, less than, not equal, less than or equal, greater than or equal, equal)
X AND Y - true if X and Y are true
X OR Y - true if X or Y or both are true
NOT X - true if X is not true
REDSTONE SIGNAL side - true if there is a redstone signal input on side
REDSTONE SIGNAL side colour - true if there is a bundled signal input on side, wire colour colour

Example program (number guessing game):

LET NumberOfGames = 0
LET NGuesses = 0

PRINT "What's your name? ";
INPUT name$
PRINT "Hi ";NAME$;"!"
IF INSTR(UPPER$(NAME$), "A", 1) <> -1 THEN PRINT "I see your name has an A in it."
PRINT

[difficulty]
PRINT "Select difficulty level - enter EASY, HARD or INSANE: ";
INPUT SEL$
LET SEL$ = UPPER$(SEL$)
IF SEL$ = "EASY" THEN LET MAX = 10:GOTO [startgame]
IF SEL$ = "HARD" THEN LET MAX = 100:GOTO [startgame]
IF SEL$ = "INSANE" THEN LET MAX = 10000:GOTO [startgame]
CLS
PRINT "That's not a valid answer, silly ";name$;"!"
GOTO [difficulty]

[startgame]
LET G = INT(RND() * MAX + 1)

PRINT
PRINT "=== GAME IS STARTING ==="

[enternumber]
PRINT "Enter a number between 1 and ";MAX;": ";
INPUT A
LET NGuesses = nguesses + 1
IF A < G THEN PRINT "Too low!"
IF A > G THEN PRINT "Too high!"
IF A <> G THEN GOTO [enternumber]

LET NumberOfGames = NumberOfGames + 1
PRINT "That's right!"
PRINT "You have won: ";NumberOfGames;" games in ";nguesses;" guesses, ";name$
GOTO [startgame]
Zudo #2
Posted 11 September 2013 - 12:28 PM
Nice :)/>
ElvishJerricco #3
Posted 11 September 2013 - 06:14 PM
I hope new languages becomes a popular thing in CC. Making or modifying a language is great fun!
Mitchfizz05 #4
Posted 11 September 2013 - 06:35 PM
Good job! It looks a bit like batch.
Actually: how about FileIO functions?
immibis #5
Posted 12 September 2013 - 05:07 AM
Updated - added AND, OR, NOT, WAIT x SECONDS and redstone signal commands: WAIT FOR REDSTONE SIGNAL side, WAIT FOR REDSTONE SIGNAL side colour, SET REDSTONE SIGNAL side ON/OFF, SET REDSTONE SIGNAL side colour ON/OFF, IF REDSTONE SIGNAL side, IF REDSTONE SIGNAL side colour, and WAIT FOR REDSTONE SIGNAL CHANGE

Also renamed to NextBASIC because next is an actual word.

Trivia: You can use variable names that are the same as commands.

INPUT IF
IF IF = 5 THEN PRINT "You typed 5!"
More trivia: NextBASIC usually doesn't care about spaces.

PRINT"Enter two numbers"
INPUTIF
INPUTTHEN
IFTHEN=IF THENPRINT"They're the same!"
kreezxil #6
Posted 12 September 2013 - 05:17 AM
You Sir are a MAD MAN and I commend thee! Hear hear!!

I give you 5 :D/> :D/> :D/> :D/> :D/> for both advancing CC in lua to create this and setting the modern programmer back 30 years!

I might actually write something in this dialect now that you've gone and made it. Ironically I was thinking about making something like this a week ago, but alas implementation is not in the scope of a professional procrastinator.
Yevano #7
Posted 12 September 2013 - 09:57 PM
Could be nice to have an interfacing feature between this and Lua. For example, "LCALL lua_func args" to call a Lua function. This way you don't have to make a command for each and every CC library function.