]> ocean-lang.org Git - ocean-D/blob - Ocean-functions
updates
[ocean-D] / Ocean-functions
1 I want to add functions and procedures soon.  I should decide on syntax
2 at least.
3
4 The args to a function are effectively a struct, so I want it to look
5 the same.  C doesn't allow "int a, b, c" in the parameters, which I
6 think is clumsy.  struct can be
7
8   struct name:
9      a,b,c:number
10      d:string
11
12 So function might be
13
14    func name:
15        arg1, arg2: type
16        arg3: type2
17    return type
18    do:
19         stuff
20
21 A procedure is different as it doesn't have just a return type,
22 it has a return structure.  So many C functions have 'ret' or 'result'
23 variable that it might be nice to follow the Pascal approach of
24 assigning to the function name??  or having
25
26     func name
27        args:types
28     return
29        results:types
30     do
31        statements
32
33 A shorter version would be
34
35     func name(args:types;args:types):type { }
36 or
37     func name(args:types;args:types):(result:type;...) {}
38
39
40 Can arguments be optional?
41  - yes, if "= value" follows the type
42 Can we have variable number of args
43  - yes. Any arg can be an inline-array. The last arg can
44    have undefined length [].
45 Can named args be passed in
46  - yes, once I have a syntax for manifest structs
47    
48 Arrays seem very different from structs.
49  - they don't have names (as types)
50  - they don't need grouping in function params??
51 Does this make any sense?
52 Maybe I should require the grouping
53   printf("%d %d %d", (1,2,3))
54 ??
55
56
57 Functions can be used before being declared - analysis happens after all is read.
58
59 'main' is given an array of strings of unknown length.
60 So I need the formal parameter to accept variable-length arrays.
61 Do I use
62    argv:[] string
63 and support "argv.len", or do I use
64    argv:[argc] string
65 which instantiates argc with the length?
66 Probably the latter.
67
68 Arrays have to be passed by reference, they are const if :: used for type.
69 Structs .. ditto.
70 Strings and numbers are immutable.
71
72 So I need functions to turn strings into number or bool.
73 Just bool() and number() for now.
74 So
75 DONE 1/ change 'program' to make everything a string, and use functions to get numbers.
76 DONE 2/ change 'program' to receive an array with bindably size
77 DONE 3/ change 'program' to 'func main' etc
78 DONE4/ allow more functions.
79
80 What syntax to use for formal types?
81 I will need type parameterisation some day.  For now I just need the
82 size of an array to be parameterised.
83   argv:[argc]string
84 looks like normal type.  But it isn't clear if argc is a global constant
85 or a variable.
86   argv:[argc:int]string
87 seem redunant because of course the array index is an int.
88   argv:[argc:]string
89 might be acceptable.
90 It is like leaving out the type is
91    a := 4
92
93 So let's go with that.
94
95
96 DONE To handle functions, and recursion in particular, I need a concept of a
97 stack frame in the interpreter.  This needs to include room for
98 parameters and results and local variables.  So a function needs to know
99 the size of its frame, and each variable needs an offset.
100 So I need to switch 'program' over to this approach before adding
101 functions.
102 Then the function frame will have a results frame separate from the
103 main frame, as it can last longer.  So each variable needs to be marked
104 as 'result' or not.