Skip to content

Commit 8aef304

Browse files
committed
import split
1 parent 32084f3 commit 8aef304

14 files changed

+390
-93
lines changed

doc/language/builtin/string.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# string
2+
3+
## Big Endian layout
4+
5+
```
6+
struct string {
7+
u64 _data
8+
u64 _size
9+
u64 _capacity
10+
};
11+
12+
fn is_short(let self := string) -> bool {
13+
return (self._capacity & 1) == 0
14+
}
15+
16+
fn capacity(let self := string) -> u64 {
17+
if (self.is_short()) {
18+
return 23
19+
} else {
20+
return self._capacity >> 1
21+
}
22+
}
23+
24+
fn size(let self := string) -> u64 {
25+
if (self.is_short()) {
26+
return 23 - (self._capacity >> 1)
27+
} else {
28+
return self._size
29+
}
30+
}
31+
32+
fn data(let self := string) -> ptr[char] {
33+
if (self.is_short()) {
34+
return std.address_of(char, self)
35+
} else {
36+
return self._ptr
37+
}
38+
}
39+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# C-ABI Compatibility
2+
3+
4+
Integers
5+
--------
6+
7+
Hikolang's `int` type is compatible with C-style integers,
8+
including layout and size in memory.
9+
10+
The following integers are compatible with the C-API.
11+
12+
| hikolang-type | hikolang alias | C-type
13+
|:--------------------------------------------------|:---------------|:---------
14+
| int[-128..=127] | std.i8 | std::int8_t
15+
| int[0..=255] | std.u8 | std::uint8_t
16+
| int[-32768..=32767] | std.i16 | std::int16_t
17+
| int[0..=65535] | std.u16 | std::uint16_t
18+
| int[-2147483648..=2147483647] | std.i32 | std::int32_t
19+
| int[0..=4294967295] | std.u32 | std::uint32_t
20+
| int[-9223372036854775808..=9223372036854775807] | std.i32 | std::int32_t
21+
| int[0..=18446744073709551615] | std.u32 | std::uint32_t
22+
| int[0..=255] | std.c_bool | std::bool
23+
| int[0..=255] | std.c_byte | std::byte
24+
| | std.c_char | char
25+
| int[-128..=127] | std.c_ichar | signed char
26+
| int[0..=255] | std.c_uchar | unsigned char
27+
| | std.c_ishort | signed short
28+
| | std.c_ushort | unsigned short
29+
| | std.c_iint | signed int
30+
| | std.c_uint | unsigned int
31+
| | std.c_ilong | signed long
32+
| | std.c_ulong | unsigned long
33+
| | std.c_illong | signed long long
34+
| | std.c_ullong | unsigned long long
35+
| | std.c_isize | std::ptrdiff_t
36+
| | std.c_usize | std::size_t
37+
| | std.c_ptr | std::intptr_t
38+
| | std.c_ptr | std::uintptr_t
39+
| | std.c_ptr | _type_ *

doc/language/syntax/argument_declaration.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
## Syntax
44

5-
[_binding_mode_](binding_mode.md)__?__ [_name_](name.md) [_type_declaration_](type_declaration.md)__?__ _default_value_declaration_**?** __|__\
6-
[_binding_mode_](binding_mode.md)__?__ [_name_](name.md)`...` [_type_declaration_](type_declaration.md)__?__
5+
[_binding_mode_](binding_mode.md)__?__ [_name_](name.md) [_type_declaration_](type_declaration.md)__?__
6+
__(__ `=` [_expression_](expression.md) __)?__ __|__\
77

8+
[_literal_](literal.md) [_type_declaration_](type_declaration.md)__?__ __|__\
89

9-
### default-value-declaration
10+
`(` [_expression_](expression.md) `)` [_type_declaration_](type_declaration.md)__?__ __|__\
1011

11-
`=` [_expression_](expression.md)
12+
[_binding_mode_](binding_mode.md)__?__ [_name_](name.md)`...` [_type_declaration_](type_declaration.md)__?__
1213

1314

1415
## Semantics
1516
Declares an argument for a function or lambda with a [_name_](name.md)
1617
which can be used as a variable inside the function's or lambda's code-block.
1718

19+
If the argument is a [_literal_](literal.md) or an [_expression_](expression.md)
20+
between `(` and `)`, then the argument is a constant. This allows functions
21+
to be specialized when a specific argument is passed.
22+
1823
The type declaration is optional. If not specified, the type is inferred from
1924
the types passed in the [_function-call_](function_call.md). If the type is
2025
specified, the type-expression must be elaborated to a concrete type at

doc/language/syntax/attributes.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,65 @@
22

33
## Syntax
44

5-
### Optional attribute
6-
75
[_fully_qualified_name_](fully_qualified_name.md) __(__ `(` [_argument_list_](argument_list.md) `)` __)?__
86

9-
### Required attribute
7+
## Semantics
108

11-
[_fully_qualified_name_](fully_qualified_name.md) __(__ `(` [_argument_list_](argument_list.md) `)` __)?__
9+
If the [_fully_qualified_name_](fully_qualified_name.md) is a single name,
10+
then this attribute is required. A required attribute must be handled by the compiler.
11+
If the compiler does not know how to handle the attribute it is an error.
1212

13-
## Semantics
13+
If the [_fully_qualified_name_](fully_qualified_name.md) has the `std` prefix
14+
it is a well-known optional attribute. These attributes don't change the semantic
15+
meaning of the program, instead they are directives to the compiler to create
16+
better code.
17+
18+
Other prefixes are attributes that are handled by specific compilers. Compilers
19+
should ignore any attributes that do not match their own prefix.
20+
21+
### abi()
22+
Make this function available for linking with an application written in a
23+
different language; using a different ABI (Application Binary Interface).
24+
25+
- __c__: Use the C ABI.
26+
- __cpp__: Use the C++ ABI.
27+
28+
### no_return
29+
This function will not return.
30+
31+
An implementation must terminate the application if the function does
32+
return. Or proof that the function will not return.
33+
34+
### pre()
35+
Pre-condition is checked before calling the function.
36+
37+
### post()
38+
Post-condition is checked after calling the function.
39+
40+
41+
### std.no_inline
42+
Calls to this function are never inlined.
43+
44+
This is a strong optimization lever:
45+
- It reduces code size by not inlining the code, which will reduce
46+
code-cache usage.
47+
- It significantly reduce register pressure, which will reduce the
48+
amount of reads/writes to memory. And may in turn reduce size of the
49+
code; which in turn may allow more inlining.
50+
51+
### std.depricated, std.depricated()
52+
This function is depricated print a warning. A reason for deprication can
53+
be passed as a constant-string.
54+
55+
### std.discard
56+
The return value of this function is allowed to be discarded by the caller.
57+
58+
Discarding a return value is:
59+
- The return value is not assigned to a variable.
60+
- The return value is not passed to a function.
61+
- The return value is not part of a larger expression.
62+
- The return value is not used as an expression to a control-expression.
63+
64+
If this attribute isn't set on a function, the compiler must give a warning
65+
when the function's return value is discarded.
1466

doc/language/syntax/function_definition.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
## Syntax
44

5-
`function` [_name_](name.md) `(` [_argument-list_](argument_list.md) `)`\
6-
*function-attribute*__*__ \
5+
`fn` [_name_](name.md) `(` [_argument-list_](argument_list.md) `)`
76
[_result-type-declaration_](result_type_declaration.md)__?__\
7+
*attribute*__*__ \
88
`{` [_statement-list_](statement_list.md) `}`
99

10-
### function-attribute
11-
`pre` `(` [_expression_list_](expression_list.md) `)` __|__\
12-
`post` `(` [_expression_list_](expression_list.md) `)` __|__\
1310

1411
## Semantics
1512

doc/language/syntax/import_declaration.md

Lines changed: 0 additions & 77 deletions
This file was deleted.

doc/language/syntax/import_fn_c.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# import-fn
2+
3+
## Syntax
4+
5+
`import` `fn` `c` [_name_](name.md) `(` [_argument-list_](argument_list.md) `)` [_result-type-declaration_](result_type_declaration.md) `;`

doc/language/syntax/import_git.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# import-git
2+
3+
## Syntax
4+
5+
`import` `git` _git-url_ *git-rev*__?__ [_compile_condition_](compile_condition.md)__?__ `;`
6+
7+
git-url := [_string_literal_](string_literal.md)
8+
9+
git-rev := [_string_literal_](string_literal.md)
10+
11+
12+
## Semantics
13+
14+
Clone a git repository into `.hkdeps/<name>-<hash>/` of the host-repository.
15+
Modules located in the git repository can then be imported into any module
16+
in the current repository.
17+
18+
The first _string_literal_ is the URL of the git repository, and the second
19+
_string_literal_ is the [git-rev](https://git-scm.com/docs/git-rev-parse.html)
20+
to checkout.
21+
22+
When the `.hkdeps/<name>-<hash>/` directory for a repository does not yet
23+
exist; the compiler will clone the repository into that directory then
24+
checkout the git-rev.
25+
26+
Otherwise when the `.hkdeps/<name>-<hash>/` directory contains a clone
27+
of the repository and points to the same git-rev and the git-rev is a
28+
points to a _tag_ or a _sha_. Then this means that the correct revision
29+
is checked out, so nothing is changed.
30+
31+
Otherwise when the `.hkdeps/<name>-<hash>/` directory contains a clone
32+
of the repository and points to a different git-rev or the git-rev points
33+
to a _branch_. The repository is cleaned by removing files that are not
34+
part of the repository, then the remote is fetched and the git-rev is
35+
checked out.
36+
37+
A failure to clone is a warning, it is possible that other checkouts may
38+
satisfy imported packages.
39+
40+
A failure to update is an error, because it may mean that local
41+
modifications of files are conflicting with the update.
42+
43+
A _tag_ is expected to not change and therefor no update is done. You
44+
can override this using the `--force-update` compiler option.
45+
46+
A cleanup is needed when the source files are updated, as out-of-repository
47+
files had been created by those source files. With the `--force-clean` will
48+
clean these files even if the repository has not been updated.
49+

doc/language/syntax/import_lib.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# import-lib
2+
3+
## Syntax
4+
5+
`import` `lib` _lib-name_ [_semantic_version_]__?__ [_compile_condition_](compile_condition.md)__?__ `;`
6+
7+
lib-name := [_string_literal_](string_literal.md)
8+
9+
## Semantics
10+
11+
Link any applications that imports a module or its package recrusively to
12+
the library named _lib-name_.
13+
14+
The library is either a archive/static library or a dynamic/shared library
15+
and is found in the library path.
16+
17+
The _lib-name_ is the base name, it excludes both the prefix `lib` and the
18+
extensions: `.so`, `.a`, `.lib`, `.dll`.
19+
20+
An optional _semantic-version_ is used to select a specific version of a
21+
library.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# import
2+
3+
## Syntax
4+
5+
`import` [_fully_qualified_name_](fully_qualified_name.md) __(__ `as` [_identifier_](identifier.md) __)?__
6+
[_compile_condition_](compile_condition.md)__?__ `;`
7+
8+
## Semantics
9+
10+
Imports a module, so that its functions, types and variables can be used in the
11+
current file.
12+
13+
The [_fully_qualified_name_](fully_qualified_name.md) is the name of the module
14+
to import. The optional `as` clause allows the module to be made available under
15+
a shorter name to make the code more readable.
16+
17+
If the module is located in a different repository, then the repository must be
18+
imported using a `git` or `zip` by at least one file in the current repository.
19+
Likely this in done in a `application` or `package` file.
20+
21+
When you import a module all sub-modules are imported as well; accessible through
22+
the sub-names. This holds when the module is renamed through the `as` part.
23+

0 commit comments

Comments
 (0)