Philip-code(TM)
Published in
2 min readSep 23, 2018
Philip-code(TM) or just pcode, is my own dialect of pseudo-code which I use for the “Computer programming — and so can you!” series.
It has the standard operators +,-,*,/ and so on.
Apart from those it consists of:
- var: a variable declation, consists of var [name] : [typename]
- <=: variable assignment. For example: x <= 10. Assign 10 to x.
- {} : defines a list. For example {1,2,3} is the list containing 1,2 and 3.
- If … then … else … end if: If a condition is true execute the code after then, if not, execute the code after else. For example:
If i > 2 then
print(“I was greater than 2”)
else
print("I was less than or equal to 2")
End if - For: A for loop which assigns a variable and every time the loop runs it increments that variable. The loop runs for as long as a condition is true. For example
For i <= 0, i<10, i<=i+1 … End For
. This assigns the variable the value 0, runs as long as i < 10, and every time the loop runs it assigns i the value i+1. - mod The modulo operator, returns the remainder after integer division. For example: 5 mod 2 = 1 because 5/2=2 (integer division).
- (…) The n-tuple. For example
(1,2)
is a 2-tuple consisting of 1,2 (a pair). The idea of a pair can be extended to the n-tuple, for example(4,3,6,3,30)
is a 5-tuple. Is declared asvar t : Tuple<int,int,int,int,in>.
To get an item out of a tuple index it like a list:t[2]
. A tuple is just a collection of variables that always go together. - Func: Declares a function. For example:
Func f(i : int, s : string) :String
is a function taking two arguments/parameters called i of type integer and s of type string and the return type is a string. To return a value from a function, use the return keyword.
An example of pcode which is meant to be self-explanatory (with the comments).
# Philip-code(TM) - This is a comment, which is ignoredvar i : Integer # Declares a variable i of the type Integer
var s : String # Declares a variable s of the type String
var j : Integer
var list : List<Integer> # List of integerss <= "Hello" # <= is the assignment operator. It assigns "Hello" to
# s in this case.
i <= 10 # Assigns 10 to i
list = {10,20,30,40,50,60,70,80,90,100}# This is a for loop where j starts at 0,it runs as long as j<10,
# and every time the loop reaches the end j is increased by 1
# (incremented).For j <= 0, j < 10, j<=j+1
print("J is: ")
print(j)
# mod is the modulo operator giving the remainder of division.
# sometimes written as %
if j mod 2 = 0 then
print("j is even")
else
print("j is odd")
end if print("List at this position: ")
print(list[j])
End ForFunc addNumbers(i : int, j : int) : int
return i + j
End func