Scrap Mechanic

Scrap Mechanic

Not enough ratings
New Programmable Processor Language
By wingcomstriker405
This is a detailed guide for the new programmable processor language. It will explain all aspects that are currently implemented.
   
Award
Favorite
Favorited
Unfavorite
Introduction
The new programming language is a small scripting language to allow more complex, efficient and faster programs. In the following guide ill show the syntax, the system functions and the datatypes that are supported. I will also show some examples and explain what i try to achive in the future.
Syntax
In this section i explain how the syntax looks and what features are supported.
  • Functions
  • For-Loop
  • While-Loop
  • If-Statements
  • Elseif-Statements
  • Else-Statements
  • Variables
Functions
A function is a small chunk of code that can be called during a program. It allows to write shorter and more expressive code. It is possible to pass values as parameters to a function. It is also possible to return a value from the function to the location that it has been called from.
The general form of a function look like this:
def <functionname>(<parameters>) <code> end
def is the keyword for a function. The function name can be pretty much anything you like. The parameters are basically variables that get the values assigned that are passed to the function. You can have one or more parameters. After the function signature the actual function code follows. To finish a function you write end.

For-Loops
This loop has a built in counter. The general form of the for loop looks like this:
for <code1>, <code2>, <code3> <code> end
for is the keyword for a for loop. The loop declaration consist of three code segments. The first code segment is executed once before the loop starts. The second code segment is a condition that needs to be evaluated to a boolean. As long as the condition evaluates to true the loop runs. The third code segment is executed once after each iteration of the function. To finish the for loop you write end just like the function.

While-Loops
A while loop is like a for loop but with just a condition. The general form of a while loop looks like this:
while <code1> <code> end
The keyword is while. The first code segment is the condition of the loop. Like every condition in this language it needs to evaluate to a boolean and as long as the condition is true the loop runs. After the condition the code inside the function follows. To finish the loop you write end.

If-Statement
The if statement is used to check conditions. The general form looks like this:
if <code1> <code> end
The keyword is if. The code segment in the declaration is the condition that needs to be true to execute the code inside the statement. At the end again end.

Elseif-Statement
The elseif statment is used to check a condition when the if or elseif statement before it was false.
elseif <condition> <code> end


Else-Statement
The else statement is executed when all if and elseif statements before are false.
else <code> end

Note: You only need one end after all if, elseif and else statements.
if <condition> <code> elseif <condition> <code> else <code> end
System Functions
Now i want to give an overview of all the functions that are supported in the core language. There are a lot of function in the core language. They are used to speed up the program and get data from the world that the processor is located in.

num(<string>) - converts a string into a number
str(<value>) - converts a value to string
vec(<number>, <number>, <number>) - returns a vector object
add(<vector>, <vector>) - adds the two vectors
sub(<vector>, <vector>) - subtracts a vector from another one
dot(<vector>, <vector>) - returns the dot product of the two vectors
cross(<vector>, <vector>) - returns the cross product of the vectors
angle(<vector>, <vector>) - returns the angle between the vectors
scale(<vector>, <number>) - scales the vector
length(<value>) - returns the length of a vector or a list
normalize(<vector>) - returns the normalized vector
combine(<list>, <list>) - returns a list containing all number indexed elements of both lists
keys(<list>) - returns a list of all keys of a list
values(<list>) - returns a list of all values of a list
in(<string>, <number>) - returns the value of the specified input
out(<value>, <string>, <number>) - outputs a value to the specified output block
print(<value>) - prints out a value on the screen
pos() - returns the position of the computer in the world
dir() - returns the direction that the front side of the computer faces
vel() - returns the velocity of the computer
min(<number>, <number>) - returns the smaller of the two numbers
max(<number>, <number>) - returns the bigger of the two numbers
abs(<number>) - returns the absolut value of the number
cos(<number>) - returns the cosine of the number
sin(<number>) - returns the sine of the number
tan(<number>) - returns the tangent of the number
acos(<number>) - returns the acosine of the number
asin(<number>) - returns the asine of the number
atan(<number>) - returns the atangent of the number
random() - returns a random number
deg(<number>) - converts radians to degree
rad(<number>) - converts degree to radians
floor(<number>) - floors a number
ceil(<number>) - ceils a number
exp(<number>) - returns e to the power of the number
seed(<number>) - sets the seed of the random number generator
time() - returns the unix time
type(<value>) - returns the type of the value
open(<string>) - returns a file iterator object
next() - returns the next line of a file iterator
line() - sets the current line of file iterator
stream(<string>) - returns a stream to the file
clear(<stream>) - clears the contents of the stream
filetype(<value>) - returns the type of a fileiterator or stream
filename(<value>) - returns the name of a fileiterator or stream
current(<fileiterator>) - returns the current line of a file iterator
write(<stream>, <string>) - writes a string to a stream
insert(<stream>, <number>, <string>) - inserts a string into the contents of a stream
remove(<string>) - removes a file with the specified name
new(<string>, <string>) - creates a new file with the specified name and file type
exists(<string>) - checks if a file with the given name exists
matches(<string>, <string>) - checks if a string matches a pattern
substring(<string>, <number>, <number>) - returns the substring
replace(<string>, <string>, <string>) - replaces all occurences of a pattern with the string
char(<number>) - converts the number into a character
sleep(<number>) - sleeps the specified number of ticks
round(<number>) - rounds the number
pack(<string>, <list>) - returns a package with all values that fit the specified type
transfer(<package>, <string>, <number>) - sends the package to a distributor block
Datatypes
The language supports different datatypes. Some of these datatypes have some restrictions that i want to explain here.
Datatypes
  • Numbers
  • Booleans
  • Strings
  • Lists
  • Vectors
  • Streams
  • Fileiterators

Numbers
Numbers can be integer or floating point numbers. Just write out the number that you want. If you want to write a negative number you need to write the sign right next to the number. Lets say you want to add -10 to 10 This example is incorrect and will lead to errors:
a = 10 + - 10
This is the correct way:
a = 10 + -10

Booleans
A boolean can be true or false. Just write true or false e.g.:
a = true

Strings
A string can be any sequence of characters. Strings start and end with a single quote e.g.:
a = 'a random string'

Lists
Lists are very useful to store data. To create a list you can just write:
a = []
This will create an empty list. It is also possible to create a list with values in it.
a = [10, 20, 30, 40]
NOTE: 10 will be at index 1. 20 at index 2 and so on.

Vectors
This datatype represents simple 3d vectors (analytic geometry). To create a new vector you need to call a system function e.g.:
a = vec(1, 2, 3)
This will return a vector object with x1 = 1, x2 = 2 and x3 = 3.

Streams
A stream is an object that allows you to write to files on the computer. This is how you create a stream to a file:
s = stream('name-of-the-file')

Fileiterator
A fileiterator allows you to read from a file. There are different ways to get the contents of the hole file one is this:
fi = open('name-of-the-file') line_count = length(fi) content = [] for i = 1, i <= line_count, i += 1 content <- next(fi) end
Examples
After all the information that i provided i want to show some examples and explain what they do in detail.
This is a super simple drone stabalization algorithm that allows you to activate thrusters depending on the roll and pitch of the drone.
Here you can download the drone (in the description are instructions on how to use the drone and the limitations of the drone).
Future Plans
Im looking into an OOP extension but im not sure how far i manage to implement a usable system.
39 Comments
Natejoestev 18 Aug, 2022 @ 12:46pm 
that is soooo Lua
wingcomstriker405  [author] 4 Jul, 2022 @ 11:59am 
c has main too ;)
BraveCaperCat 4 Jul, 2022 @ 11:52am 
main() sounds like java to me.
wingcomstriker405  [author] 2 Jul, 2022 @ 3:19pm 
Java? It would be more of a Lua / Python mix than java (imo)
BraveCaperCat 2 Jul, 2022 @ 2:23pm 
I am a great coder, and that looks like java, but not exactly.
wingcomstriker405  [author] 19 May, 2022 @ 9:10pm 
Thank you :) fyi i have released a newer processor with a newer more polished language (the integrated processor)
r8gobb8 19 May, 2022 @ 7:25pm 
Looks like python and lua had a baby. Good Job :D
wingcomstriker405  [author] 28 Mar, 2021 @ 8:16am 
Np :)
'Overdrive' 28 Mar, 2021 @ 7:13am 
I had problem with this index thingy like a[1] = True v False, a[2] = 1 v 0. Now everything is clear, thanks!
wingcomstriker405  [author] 26 Mar, 2021 @ 1:13pm 
Good to know :) have fun