school

thing1's amazing school repo
Log | Files | Refs | Submodules | README

overview (8117B)


      1 CS course work project
      2 
      3 zippy - a high level functional programing language
      4 
      5 zippy will be an interpreted language designed to replace C in higher level use
      6 C is used largely to write most of the unix desktop, however c can be difficult
      7 when writing larger programs, and is missing moddern features like oop or
      8 functional patterns (higher order functions)
      9 
     10 requirents:
     11 	- must be human readable, even a non programmer should be able to figure
     12 	out what a program is doing
     13 	- faster than competion, other languages that have tried to slot into
     14 	this position like python are far to slow, and thus it must be faster
     15 	- low runtime overhead, the interpreter should be small, it shouldn't 
     16 	force the developer to think about how the interpreter works to use it
     17 	efficently
     18 	- have moddern tools, a pkg manager and enviroment manager, making it
     19 	easy to develop for
     20 	- needs to support c libarys
     21 
     22 language info
     23 
     24 zippy will use function programming as a primary solution, however also support
     25 simple oop, with classes (no inheritence)
     26 its syntax will be comparable to lisp, with prolific use of "()"
     27 it will need a stdlib, which can be taken from C's stdlib
     28 
     29 types:
     30 
     31 	char - just like a c char
     32 	i32 - 32 bit signed int
     33 	f32 - 32 bit signed float
     34 	u32 - unsigned 32 bit int
     35 	i16- 16 bit signed int
     36 	u16 - 16 bit unsigned int
     37 	bool - TRUE/FALSE
     38 
     39 
     40 	arrays:
     41 	
     42 		arrays can be created like so
     43 		(let arr[]:char "hello there")
     44 		(let arr[]:i32 1 2 3)
     45 
     46 		all arrays are linked lists of there data type
     47 		their size is not fixed
     48 
     49 		you can get, or set the value of an array at a specific index using
     50 		arr[index]
     51 
     52 	strings:
     53 	
     54 		strings are arrays of chars
     55 		a string can be defined by a litteral like so
     56 		"hello world" - this is the same as 'h' 'e' 'l'....
     57 		because this is represented as a linked list there is no need to have a
     58 		NULL terminator like in c, the end of the string is jus the point where the 
     59 		linked list has no more nodes
     60 
     61 comments:
     62 
     63 	comments can be writen like so
     64 	! this is a comment
     65 
     66 indentation and spacing:
     67 	
     68 	indentation and new lines arent at all necesary
     69 	any new lines will be converted to spaces, and tabs will be removed
     70 	the only place that needs spaces is when calling a function and giving its args
     71 
     72 scopes:
     73 
     74 	() can be placed anywhere, and anything that is in them is in there own scope
     75 	as soon as the bracket is closed, any variables defined in the scope
     76 	will be deleted 
     77 	any scope inherets all variables and functions from the scope that contains it
     78 	code writen in the main body of the file can be imagined like so
     79 	(
     80 		...
     81 		...
     82 		...
     83 		your code
     84 		...
     85 		...
     86 		...
     87 	)
     88 	however that level of indentation is not needed
     89 	
     90 	this can be helpful if you want to have two scopes with different imports
     91 	(
     92 		(import z)
     93 		(
     94 			(import x) 
     95 			! anything writen here has access to libary x and z
     96 		)
     97 		(
     98 			(import y)
     99 			! anything writen here has access to libary y and z
    100 		)
    101 		! anything writen here has access to libary z	
    102 	)
    103 	
    104 libarys:
    105 
    106 	libarys are similar to header files in c
    107 	when imported the user can use any function contained within the file
    108 	
    109 	it would be good practice to put your libary in a struct to avoid functions with the
    110 	same names coming from 2 libarys, however its not needed
    111 
    112 errors:
    113 
    114 	errors would be 
    115 
    116 keywords:
    117 
    118 	(defun arg1:type arg2:type returnType
    119 		...
    120 		...
    121 		...
    122 	)
    123 	defines a function, can be done anywhere in the program, all functions 
    124 	must return a value, return type and arguments must be known at runtime
    125 
    126 	returns the function that can be asigned with let
    127 	
    128 	(return VALUE)
    129 	returns VALUE from a function, VALUE must be of the same tupe as returnType
    130 	in defun
    131 
    132 	(for x a b function)
    133 
    134 	runs the function inside the loop, x will be increment by 1 each iteration and
    135 	it will have a starting value of a running until it reaches b (inclusive)
    136 	x cant be changed in any way other than the for loop
    137 	x, a, b are all of type i32
    138 
    139 	(while condtion function)
    140 
    141 	runs all code inside the loop until the condion is not TRUE
    142 	condition must be a bool (TRUE/FALSE)
    143 	
    144 	(let name:type value)
    145 
    146 	assign value to variable name, this will create the variable if it doesn't already
    147 	exist, and overwrite it if it does
    148 	names type must be defined by type
    149 
    150 	(const name:type value)
    151 
    152 	same as let but creates an imutable variable
    153 
    154 	(import libary)
    155 
    156 	searches the libary dir and imports the libary to the current project
    157 	libary can also be a specific path if it starts with / or ./ or ../
    158 
    159 	(struct name
    160 		...
    161 		...
    162 		...
    163 		(defun init arg1:type type
    164 			...
    165 			...
    166 			...
    167 		)
    168 		(defun deinit arg1:type type
    169 			...
    170 			...
    171 			...
    172 		)
    173 	)
    174 
    175 	structs can be used as a custom data type
    176 	creates a struct/class called name, functions called init will be ran when an
    177 	object is created
    178 	when deinit is called the code init will be ran, then the object will be deleted
    179 	if deinit is not defined it can still be called to delete the instace of the class
    180 
    181 	you can create a struct like so:
    182 		(let mystruct:name (name.init arg1 arg2))
    183 
    184 	you can call a method attached to an instace of a struct like this:
    185 		(mystruct.method arg1 arg2)
    186 
    187 	and to destroy it you can just use this:
    188 		(mystruct.deinit arg1 arg2)
    189 	
    190 	structs can be recursive
    191 	eg:
    192 		(struct node
    193 			(defun init next:node
    194 				(let next:node next)
    195 			)
    196 		)
    197 
    198 	a struct can have default values
    199 	eg:
    200 		(struct person
    201 			(let age:i32 50)
    202 			(let name[]:char "john doe")
    203 			...
    204 			...
    205 			...
    206 		)
    207 
    208 	this will make the struct have default values
    209 
    210 	overall in simple terms structs are just like c structs but they can contain
    211 	functions
    212 
    213 	(symbol arg1:type arg2:type returntype location)
    214 
    215 	returns a function that can be called. the function is retrieved from a .o file
    216 	which can be writen in C, the args, should be the args that the function takes in
    217 	and there types
    218 	the interpreter will try to convert data to the equal type in C, however this may need
    219 	to be done manually
    220 	the location should be a string which is is the file path to the .o file
    221 
    222 	(if condition
    223 		...
    224 		...
    225 		...
    226 	)(elif condition
    227 		...
    228 		...
    229 		...
    230 	)(else
    231 		...
    232 		...
    233 		...
    234 	)
    235 		
    236 	simple if statment, if condition is TRUE run the code inside, elif runs if the condition in it
    237 	is TRUE and if the previous has not been TRUE
    238 	else will run if nothing else is TRUE
    239 
    240 	(cast TYPE variable)
    241 
    242 	returns a variable casted to type TYPE, if type is function, will return a function with no args
    243 	that returns the variable value in its starting type
    244 
    245 	conditions:
    246 		(= a b) returns TRUE if a is equal to be (works with arrays)
    247 		(!= a b) returns TRUE if a is not equal to be (works with arrays)
    248 		(> a b) returns TRUE if a > b (does not work with arrays)
    249 		(< a b) returns TRUE if a < b (does not work with arrays)
    250 		(>= a b) returns TRUE if a >= b (does not work with arrays)
    251 		(<= a b) returns TRUE if a <= b (does not work with arrays)
    252 		(! a) returns TRUE if a is FALSE (only works on bools)
    253 		(| a b) returns TRUE if a or b are TRUE (only works on bools)
    254 		(& a b) returns TRUE if a and b are TRUE (only works on bools)
    255 		(^ a b) returns TRUE if a xor b is true (only works on bools)
    256 
    257 		all these are just functions that can be used to assign values
    258 		eg:
    259 			(let mybool:bool (= a b))
    260 			! mybool will be TRUE if a = b
    261 	
    262 	mathmatical operations:
    263 		zippy uses reverse polish notation
    264 		(+ a b) returns a + b
    265 		(- a b)	returns a - b
    266 		(* a b) returns a * b
    267 		(/ a b) returns a / b, whole number eg: (/ 7 3) = 2
    268 		(/. a b) returns a / b, floating point number eg: (/. 7 3) = 2.5
    269 		(% a b) returns the remander of (/ a b)
    270 		(** a b) returns a ^ b
    271 
    272 
    273 sample code
    274 	(let fib:function (defun num:i32 i32
    275 			(if (< num 2)
    276 				(return num)
    277 			)
    278 			(else
    279 				(return (+ (fib (- num 1)) (fib (- num 2)) ))
    280 			)
    281 		)
    282 	)
    283 	(let a:i32 (fib 5))
    284 	! returns the 5th fib number
    285 
    286 stdlib:
    287 	(import io)
    288 	io contains a few types
    289 	they are:
    290 		(io.file)
    291 	io contains many io related functions
    292 	they are:
    293 
    294 	(io.write "hello %s\n" name) ! this is very similar to printf in C
    295 	(let input:char[] (io.read))
    296 	
    297 	(let file:io.file (io.open path[]:char "method"))
    298 	
    299 	(io.fwrite file:io.file "hello %s" name)
    300 	(let file[]:char (io.fread file:io.file))
    301 	(let line[]:char (io.freadline file:io.file lineno:i32))
    302