From 4419969979014a8eae67d5a5b6de39b09b868583 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 5 Jan 2025 14:10:56 -0800 Subject: Rewrite Readme --- chapter18/README.md | 200 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 146 insertions(+), 54 deletions(-) (limited to 'chapter18/README.md') diff --git a/chapter18/README.md b/chapter18/README.md index 606ac9a8..b9a51753 100644 --- a/chapter18/README.md +++ b/chapter18/README.md @@ -1,17 +1,61 @@ # Chapter 18: Functions -Some thinking on function syntax: -Function Types: +I hardly know where to begin! So many things changed here, mostly as indirect +consequences of supporting functions. + +Highlights of the changes: + +- Function types, and a rework of the type lattice diagrams. +- A distiguished `null` which can be used for both functions and struct pointers. +- Functions themselves can be parsed and evaluated; recursive functions are supported. +- Function calls are allowed, call sites can link and functions inlined. +- The top-level is now a call to function `main`, and this changed most tests. +- A top-level compile driver, which enforces optimization steps. +- A local scheduler. +- A new evaluator which relies on at least code motion, both global and local. +- A graphic animated viewer for peepholes. + +Functions are *not* closures in this chapter; that brings about far more +changes and subtle complexities than sensible for such a large chapter. + + + +## Function Types + ``` { argtype, argtype, ... -> rettype } ``` -Leading `{` character denotes the start of a function or function type. +A leading `{` character denotes the start of a function or function type, then +a list of argument types, and arrow `->` and the return type. + +Functions themselves are normal variables and assigned the same way: + +`functiontype fcn = function-typed-expr` + +and function typed variables can be inferred, so `val` and `var` on the left +hand side is fine. -Functions are normal variables and assigned the same way: +`val sq = { int x -> x*x; }` -```functiontype var = function-typed-expr``` +Internally, functions have a `TypeTuple` for the arguments, a return `Type`, +and a *function index* spelled as a `fidx` in the code. Function indices are +unique small dense integers, one per unique function. They will be mapped +1-to-1 to the final code address of the generated function code. + +Function pointers can refer to more than one function with the same signature, +and the `TypeFunPtr` tracks this as a bit set with the same tuple for signature +and return. + +Function types can be `null` just like references. + +Functions cannot return `void`, but they can return `null` and have their +return value ignored. A syntactic sugar for this may be added in the future. + + + +## Functions Functions themselves using the same syntax with as types, but filled in: @@ -47,64 +91,112 @@ optional. This allows any section of code to be "thunked" by wrapping it in Called: `just5() // Returns 5` +Functions are always anonymous. When finally assigned to a variable, they will +pick up the variable name for debugging and display purposes, but this has no +semantics meaning. Here is a function variable referring to more than one +anonymous function; the resulting function either doubles or squares: -Functions can be declared inside `struct`s, and will take a hidden `self` -argument to allow access to the struct members - basically the function becomes -a method. - -Also new this chapter, fields with a leading underscore `_` are private fields, -and only accessible to 'self'. - -```cpp -struct String { - u8[] _chars; // Private, not nullable, final, must be initialized in constructor - - // Functions declared inside of a struct become instance methods - // and have access to the struct members. - val len = { -> _chars#; } - - val at = { idx -> _chars[idx]; } - - // 'self' keyword returns an instance of itself. - val append = { String? str -> - // Appending a null appends nothing - if( !str ) return self; - - // Manual array copy _chars to cs - u8[] cs = new u8[_chars# + str.len()]; - for( int i=0; i<_chars#; i++ ) - cs[i] = _chars[i]; - - // Manual array copy str._chars to cs - for( int i=0; i x+x; } : { int x -> x*x; };` -}; +`fcn(3) // Prints either 6 or 9, depending on arg` +Functions can be recursive: +``` +val fact = { int n -> + n <=1 ? n : n*fact(n-1); +}; +return fact(4); // Returns 4! or 24` ``` -With some syntax sugar from the parser we can produce `Hello, Cliff` -with method calls. +You can early return as normal out of functions: ``` -String name = "Cliff"; -String greet = "Hello"; -return greet.append(", ").append(name); +val find = { int[] xs, int x -> + for( int i=0; i