6.7 KiB
The Core Language
A compiled language that assumes C as base reality but it also has lots of ideas taken from Jai, Go like type system, order indepent declarations using Ion algorithm. Syntax currently is whitespace significant. Made to practice language development, it's not supposed to be used. It has lot's of ideas from modern programming languages that you would not find in any compiler book. It supports modules combined with ordered independent declarations and lazy typechecking. Also runtime reflection, slices and other standard features you would find in C.
The language is currently very debuggable. It can produce readable C code with line directives. This allows you to debug the programs with Visual Studio with full source mapping, exactly like you would debug C programs.
Language example code
W :: #import "Windows.kl"
#import "base.kl"
PAGE_SIZE :: 4096
Memory :: struct
commit : SizeU
reserve: SizeU
data : *U8
reserve :: (size: SizeU): Memory
result := Memory{reserve=align_up(size, PAGE_SIZE)}
result.data = W.VirtualAlloc(
flProtect = W.PAGE_READWRITE,
dwSize = result.reserve,
flAllocationType = W.MEM_RESERVE,
lpAddress = 0)->*U8
return result
commit :: (m: *Memory, size: SizeU): Bool
commit_size := align_up(size, PAGE_SIZE)
total_commit := m.commit + commit_size
clamped_commit := clamp_top_sizeu(total_commit, m.reserve)
adjusted_commit := clamped_commit - m.commit
if adjusted_commit != 0
result := W.VirtualAlloc(
lpAddress = (m.data + m.commit)->*void,
dwSize = adjusted_commit,
flAllocationType = W.MEM_COMMIT,
flProtect = W.PAGE_READWRITE,
)
m.commit += adjusted_commit
return true
return false
// Examples that showcase language features can be found in /examples
Features
- Order independent declarations
- Module system
- #import for lazy loading(unused code is not compiled, big win)
- #load for normal loading
- Types as first class values and runtime reflection
- Any type
- Type type
- Slices
- Whitespace significant syntax
- Debuggable(emitting proper line directives)
Building
- Requires Windows, Visual Studio and Clang to be installed
- Run build.bat
Resources
Stuff that helped me a lot programming the compiler. Hopefully they also will be helpful to you!
- https://bitwise.handmade.network/ - series by Per Vognsen where he actually creates a C like language, very helpful, very hands on!
- https://hero.handmade.network/episode/code/day206/ - this episode of handmade hero started me on the entire compiler journey a long, long time ago.
- https://www.youtube.com/watch?v=TH9VCN6UkyQ&list=PLmV5I2fxaiCKfxMBrNsU1kgKJXD3PkyxO - I have rewatched this playlist many this, searching for keywords and ideas. Jonathan Blow's compiler was a big inspiration of mine when learning programming languages.
- A Retargetable C Compiler: Design and Implementation by Christopher W. Fraser and David R. Hanson - sometimes looked at this as a reference to figure stuff out. Very helpful resource on compiler construction, the way it's written reads more like documentation but you use what you have.
- https://github.com/JoshuaManton/sif - looked at this as a reference from time to time, author seems like a Jonathan Blow fan so it was a good resource informed by similar resources as I used.
- https://github.com/c3lang/c3c - I sometimes looked at C3 compiler as a reference, the author also let me use his big int library he wrote sometime in the past! Thanks a lot!
Done
- Type as a parameter to a function, alloc :: (size: U64, type: Type)
- Add token information to instructions
- [-] Mixing loads and imports leads to code duplication, is that what we want???
- print those token lines nicely
- Improve the python metaprogram
- Implementing type Any
- Runtime TypeInfo
- Proper type Type support
- Switch
- Type aliases :: should probably be strictly typed, but assigning constant values should work
- Array of inferred size
- Casting pointers to and from void should be implicit
- Multiple return values
- Add c string
- [-] Should compound resolution use an algorithm to reorder compounds to initialize all fields in order
- slices should be properly displayed in debugger
- Imports inside of import shouldn't spill outside
- Scope
- #assert that handles constants at compile time and vars at runtime
- Hex 0x42
- Rewrite where # happen,
- elif
- cast ->
- Remodel compound from call to {}
- Fix codegen renames
- Field access rewrite
- [-] Constants embeded in structs should be able to refer to other constants in that namespace without prefix
- [-] Order independent constants in structs
- [-] Fix recursive lambdas in structs
- Error message when file not found
- Better error messages when type difference
- [-] Fixing access to functions/structs, in C we cant have functons inside of structs / functions so we need to rewrite the tree
- Emitting #line
- Making sure debugger works
- We need ++ -- operators
- Arrays with size passed
- Values inited to 0 by default
- Some way to call foreign functions
- We are parsing wrong here: (t.str=(&string_to_lex.str)[i]);
- Test new operators, add constant eval for them
- lvalue, rvalue concept so we cant assign value to some arbitrary weird expression
- Passing down program to compile through command line
- More basic types
- Implementing required operations int128
- Fix casting
- More for loop variations
- Add basic support for floats
- Converting from U64 token to S64 Atom introduces unnanounced error (negates) - probably need big int
- Add basic setup for new type system
- Access through struct names to constants Arena.CONSTANT
- Enums
- Make sure pointer arithmetic works
- Initial for loop
- Enum . access to values
- Character literal
- Compiling and running a program
- Infinite for loop
- in new typesystem: Fix calls, fix all example programs
- Fix arithmetic operations in new type system
- Init statements, different kinds [+=] [-=] etc.
- Struct calls
- Operators: Bit negation, Not
- Default values in calls
- Resolving calls with default values
- Pass statement
- Lexer: Need to insert scope endings when hitting End of file
- Resolving calls with named args, with indexed args
- Structs
- Struct field access
- Struct field access with dots while compiling to arrows in c
- Typespecs should probably be expressions so stuff like would be possible :: *[32]int
- Initial order independence algorithm
- Think about compound expressions, unify with calls - maybe Thing(a=1) instead of Thing{a=1}