From 26b9d709aa579c16251796ae59defc799ccfaf39 Mon Sep 17 00:00:00 2001 From: MovingtoMars Date: Wed, 20 Jan 2016 21:27:53 +1300 Subject: [PATCH 1/2] start working on lang spec --- doc/langref.md | 197 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 140 insertions(+), 57 deletions(-) diff --git a/doc/langref.md b/doc/langref.md index 61131d20e7..2db5ce74a4 100644 --- a/doc/langref.md +++ b/doc/langref.md @@ -1,32 +1,5 @@ # Language Reference -## Primitive Numeric Types: - -zig | C equivalent | Description --------------|------------------------|------------------------------- - bool | bool | unsigned 1-bit integer - i8 | int8_t | signed 8-bit integer - u8 | uint8_t | unsigned 8-bit integer - i16 | int16_t | signed 16-bit integer - u16 | uint16_t | unsigned 16-bit integer - i32 | int32_t | signed 32-bit integer - u32 | uint32_t | unsigned 32-bit integer - i64 | int64_t | signed 64-bit integer - u64 | uint64_t | unsigned 64-bit integer - f32 | float | 32-bit IEE754 floating point - f64 | double | 64-bit IEE754 floating point - f128 | long double | 128-bit IEE754 floating point - isize | intptr_t | signed pointer sized integer - usize | uintptr_t | unsigned pointer sized integer - c_short | short | for API compatibility with C - c_ushort | unsigned short | for API compatibility with C - c_int | int | for API compatibility with C - c_uint | unsigned int | for API compatibility with C - c_long | long | for API compatibility with C - c_ulong | unsigned long | for API compatibility with C - c_longlong | long long | for API compatibility with C - c_ulonglong | unsigned long long | for API compatibility with C - ## Grammar ``` @@ -190,42 +163,152 @@ x{} = *= /= %= += -= <<= >>= &= ^= |= &&= ||= ``` -## Literals +## Types -### Characters and Strings +### Numeric Types - | Example | Characters | Escapes | Null Term | Type -----------------|----------|-------------|----------------|-----------|---------- - Byte | 'H' | All ASCII | Byte | No | u8 - UTF-8 Bytes | "hello" | All Unicode | Byte & Unicode | No | [5; u8] - UTF-8 C string | c"hello" | All Unicode | Byte & Unicode | Yes | *const u8 +``` +Type name C equivalent Description -### Byte Escapes +i8 int8_t signed 8-bit integer +u8 uint8_t unsigned 8-bit integer +i16 int16_t signed 16-bit integer +u16 uint16_t unsigned 16-bit integer +i32 int32_t signed 32-bit integer +u32 uint32_t unsigned 32-bit integer +i64 int64_t signed 64-bit integer +u64 uint64_t unsigned 64-bit integer - | Name -------|---------------------------------------- - \x7F | 8-bit character code (exactly 2 digits) - \n | Newline - \r | Carriage return - \t | Tab - \\ | Backslash - \0 | Null - \' | Single quote - \" | Double quote +f32 float 32-bit IEE754 floating point +f64 double 64-bit IEE754 floating point +f128 long double 128-bit IEE754 floating point + +isize intptr_t signed pointer sized integer +usize uintptr_t unsigned pointer sized integer + +c_short short for ABI compatibility with C +c_ushort unsigned short for ABI compatibility with C +c_int int for ABI compatibility with C +c_uint unsigned int for ABI compatibility with C +c_long long for ABI compatibility with C +c_ulong unsigned long for ABI compatibility with C +c_longlong long long for ABI compatibility with C +c_ulonglong unsigned long long for ABI compatibility with C +``` + +### Boolean Type +The boolean type has the name `bool` and represents either true or false. + +### Function Types +TODO + +### Array Types +TODO +Also, are there slices? + +### Struct Types +TODO + +### Pointer Types +TODO + +### Unreachable Type +The unreachable type has the name `unreachable`. TODO explanation + +### Void Type +The void type has the name `void`. TODO explanation + + +## Expressions + +### Literals + +#### Character and String Literals +``` +Literal Example Characters Escapes Null Term Type + +Byte 'H' All ASCII Byte No u8 +UTF-8 Bytes "hello" All Unicode Byte & Unicode No [5; u8] +UTF-8 C string c"hello" All Unicode Byte & Unicode Yes const u8 +``` + +``` +Escape Name + +\xNN hexadecimal 8-bit character code (exactly 2 digits) +\n Newline +\r Carriage return +\t Tab +\\ Backslash +\0 Null +\' Single quote +\" Double quote +``` ### Unicode Escapes - | Name -----------|----------------------------------------------- - \u{7FFF} | 24-bit Unicode character code (up to 6 digits) + Escape | Name +------------|----------------------------------------------- + \u{NNNNNN} | hexadecimal 24-bit Unicode character code (up to 6 digits) -### Numbers +#### Numeric Literals - Number literals | Example | Exponentiation ---------------------|-------------|--------------- - Decimal integer | 98222 | N/A - Hex integer | 0xff | N/A - Octal integer | 0o77 | N/A - Binary integer | 0b11110000 | N/A - Floating-point | 123.0E+77 | Optional - Hex floating point | TODO | TODO +``` +Number literals Example Exponentiation + +Decimal integer 98222 N/A +Hex integer 0xff N/A +Octal integer 0o77 N/A +Binary integer 0b11110000 N/A +Floating-point 123.0E+77 Optional +Hex floating point TODO TODO +``` + +### Identifiers +TODO + +### Declarations +Declarations have type `void`. + +#### Function Declarations +TODO + +#### Variable Declarations +TODO + +#### Struct Declarations +TODO + +#### Enum Declarations +TODO + + +## Built-in Functions +Built-in functions are prefixed with `@`. + +### Typeof +TODO + +### Sizeof +TODO + +### Overflow Arithmetic +Overflow arithmetic functions have defined behavior on overflow or underflow. TODO what is that behaviour? + +The functions take an integer (TODO float?) type, two variables of the specified type, and a pointer to a variable of the specified type where the result is stored. The functions return a boolean value: true of overflow/underflow occurred, false otherwise. + +``` +Function Operation +bool add_with_overflow(type, a: type, b: type, x: &type) *x = a + b +bool sub_with_overflow(type, a: type, b: type, x: &type) *x = a - b +bool mul_with_overflow(type, a: type, b: type, x: &type) *x = a * b +``` + +### Memory Operations +TODO memset and memcpy + +### Value Count +TODO + +### Max and Min Value +TODO From 361531891f3ec3b4e55ee8b10325fe693c581918 Mon Sep 17 00:00:00 2001 From: MovingtoMars Date: Wed, 20 Jan 2016 21:32:11 +1300 Subject: [PATCH 2/2] rename value_count to member_count rename BuiltinFnIdValueCount to BuiltinFnIdMemberCount rename value_count to member_count --- src/all_types.hpp | 2 +- src/analyze.cpp | 2 +- src/codegen.cpp | 4 ++-- test/run_tests.cpp | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/all_types.hpp b/src/all_types.hpp index 351b1847c5..3c23d62633 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -850,7 +850,7 @@ enum BuiltinFnId { BuiltinFnIdSizeof, BuiltinFnIdMaxValue, BuiltinFnIdMinValue, - BuiltinFnIdValueCount, + BuiltinFnIdMemberCount, BuiltinFnIdTypeof, BuiltinFnIdAddWithOverflow, BuiltinFnIdSubWithOverflow, diff --git a/src/analyze.cpp b/src/analyze.cpp index b7ec217c17..1b4c6b3af5 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2702,7 +2702,7 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry case BuiltinFnIdMinValue: return analyze_min_max_value(g, import, context, node, "no min value available for type '%s'", false); - case BuiltinFnIdValueCount: + case BuiltinFnIdMemberCount: { AstNode *type_node = node->data.fn_call_expr.params.at(0); TypeTableEntry *type_entry = analyze_type_expr(g, import, context, type_node); diff --git a/src/codegen.cpp b/src/codegen.cpp index 2d0caf8440..e97ad9a1e7 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -315,7 +315,7 @@ static LLVMValueRef gen_builtin_fn_call_expr(CodeGen *g, AstNode *node) { zig_unreachable(); } } - case BuiltinFnIdValueCount: + case BuiltinFnIdMemberCount: { assert(node->data.fn_call_expr.params.length == 1); AstNode *type_node = node->data.fn_call_expr.params.at(0); @@ -2558,7 +2558,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn_with_arg_count(g, BuiltinFnIdSizeof, "sizeof", 1); create_builtin_fn_with_arg_count(g, BuiltinFnIdMaxValue, "max_value", 1); create_builtin_fn_with_arg_count(g, BuiltinFnIdMinValue, "min_value", 1); - create_builtin_fn_with_arg_count(g, BuiltinFnIdValueCount, "value_count", 1); + create_builtin_fn_with_arg_count(g, BuiltinFnIdMemberCount, "member_count", 1); create_builtin_fn_with_arg_count(g, BuiltinFnIdTypeof, "typeof", 1); create_builtin_fn_with_arg_count(g, BuiltinFnIdAddWithOverflow, "add_with_overflow", 4); create_builtin_fn_with_arg_count(g, BuiltinFnIdSubWithOverflow, "sub_with_overflow", 4); diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 91cfd66317..2a712dfc1a 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1082,11 +1082,11 @@ pub fn main(args: [][]u8) i32 => { print_str("BAD\n"); } - if (@value_count(Foo) != 3) { + if (@member_count(Foo) != 3) { print_str("BAD\n"); } - if (@value_count(Bar) != 4) { + if (@member_count(Bar) != 4) { print_str("BAD\n"); }