Skip to content

Commit

Permalink
Added implicit destruct to assignment; Updated README.
Browse files Browse the repository at this point in the history
  • Loading branch information
NICUP14 committed Oct 1, 2024
1 parent c269c34 commit 4b077ac
Show file tree
Hide file tree
Showing 20 changed files with 432 additions and 162 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ python mlpx -C tests/test build and run
### Creating projects

> [!IMPORTANT]
> The ML project creator utility (`mlpx init`) will be available soon...
> It's recommended to use the `mlpx` build tool as it's specifically designed for this purpose: no libary redundancy and no configuration compared to using the `make` build tool.
Creating a `MiniLang` project is easy and straight-forward using the `mlpx` utility, which provides two ways with differing build tools.

```txt
# I. Copy project skeleton
cp -r skel <PATH_TO_PROJ>
# Using the mlpx build tool
python mlpx init my_new_project
# If using the GNU make build tool instead of mlpx
# II. Configure makefile parameters (ML, MLLIB)
$EDITOR <PATH_TO_PROJ>/Makefile
# Using the make build tool
python mlpx makefile-init my_new_project
```

### Build tools
Expand Down
2 changes: 2 additions & 0 deletions include/stdlib/builtin/alloc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ macro alloc_stop
literal("#define s_malloc malloc")
literal("#define s_realloc realloc")
literal("#define s_free free")
literal("#include <sdsalloc.h>")

gc_stop(&ml_gc)
_gc_running = false
Expand All @@ -39,6 +40,7 @@ macro alloc_start(_lit)
literal("#define s_malloc ml_malloc")
literal("#define s_realloc ml_realloc")
literal("#define s_free ml_free")
literal("#include <sdsalloc.h>")

gc_start(&ml_gc, &_lit)
_gc_running = true
Expand Down
12 changes: 6 additions & 6 deletions include/stdlib/c/cstdarg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ fun destruct(arg: va_list&)
va_end(move(arg))
end

macro va_start(list, param)
macro va_start(_list, _param)
# Hacky fix, literal(param) should be void
c_va_start(move(list), cast("int64", literal(param)))
c_va_start(move(_list), cast("int64", literal(_param)))
end

macro va_arg_voidptr(list)
c_va_arg(move(list), literal("void*"))
macro va_arg_voidptr(_list)
c_va_arg(move(_list), literal("void*"))
end

macro va_arg_int64(list)
cast("int64", c_va_arg(move(list), literal("long long")))
macro va_arg_int64(_list)
cast("int64", c_va_arg(move(_list), literal("long long")))
end

fun va_arg(list: va_list, argx: void*): void*
Expand Down
66 changes: 66 additions & 0 deletions include/stdlib/str_build.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import stdlib.c.cstdlib
import stdlib.string

struct str_build
str_build_len: int64
str_build_size: int64
str_build_cs: c_str
end

fun str_build(size: int64)
let cs = alloc_size(size, true)
ret str_build(0, size, cs)
end

fun len(sb: str_build&)
ret sb.str_build_len
end

fun capacity(sb: str_build&)
ret sb.str_build_size
end

fun to_str(sb: str_build&)
ret str(sb.str_build_cs)
end

fun to_cstr(sb: str_build&)
let dupl = alloc_size(sb.len)
let src = sb.str_build_cs
memcpy(src, dupl, sb.str_build_len)

ret dupl
end

fun append(sb: str_build&, ch: int8)
if sb.len == sb.capacity
panicf("Attempt to append character %c to max-capacity str_build\n", ch)
end

sb.str_build_cs[sb.str_build_len] = ch
sb.str_build_len = sb.str_build_len + 1

ret sb
end

fun append(sb: str_build&, cs: int8*)
let cs_len = strlen(cs)
if sb.len + cs_len == sb.capacity
panicf("Attempt to append c string '%s' to max-capacity str_build\n", cs)
end

for it in range(cs_len)
sb.append(cs[it])
end

ret sb
end

fun append(sb: str_build&, s: str&)
let s_len = s.len
if sb.len + s_len == sb.capacity
panicf("Attempt to append string '%s' to max-capacity str_build\n", c_str(s))
end

ret sb.append(c_str(s))
end
38 changes: 35 additions & 3 deletions include/stdlib/str_list.ml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import stdlib.macro
import stdlib.debug
import stdlib.string
import stdlib.c.cstdarg

macro args_to_str(_arg)
to_str(_arg)
end

# Converts each argument of he list to string
macro args_to_str(_arg, _other)
to_str(_arg), args_to_str(_other)
end

# Stores information retuned by "str_list_start"
# Stores an array of strings
struct str_list
str_list_size: int64
str_list_cnt: int64
str_list_arr: str*
end
Expand All @@ -24,7 +29,7 @@ fun _str_listv(cnt: int64, listx: va_list)
arr at it = str(arg)
end

ret str_list(cnt, arr)
ret str_list(cnt, cnt, arr)
end

fun _str_list(cnt: int64, ...)
Expand All @@ -39,7 +44,34 @@ fun str_list(cnt: int64, listx: va_list)
end

macro str_list_from(args)
_str_list(count(args), args_to_str(args))
_str_list(count(args), count(args), args_to_str(args))
end

fun len(list: str_list&)
ret list.str_list_cnt
end

fun capacity(list: str_list&)
ret list.str_list_size
end

fun append(list: str_list&, arg: c_str)
if list.len == list.capacity
panicf("Attempt to append c string '%s' to max-capacity str_list\n", arg)
end

list.str_list_arr[list.str_list_cnt] = arg.str
list.str_list_cnt.incr

ret list
end

fun append(list: str_list&, arg: str&)
if list.len == list.capacity
panicf("Attempt to append string '%s' to max-capacity str_build\n", c_str(arg))
end

ret list.append(c_str(arg))
end

# For-loop support
Expand Down
6 changes: 4 additions & 2 deletions include/stdlib/string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ end

# Create an sds string from a boolean value.
fun to_str(value: bool): str
let cs = null
if value
ret str("true")
cs = "true"
else
ret str("false")
cs = "false"
end
ret str(cs)
end

# Create an sds string from a long long value.
Expand Down
4 changes: 3 additions & 1 deletion mlpx
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ def handle_cmd_list(args: List[str]):
if len(parts) > 0:
handle_cmd(parts)
else:
print_error(f'Missing command in list {" ".join(args)}')
# Defaults to build and run for convenience
handle_build([])
handle_run([])


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion samples/printf/src/printf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fun custom_printf(format: int8*, ...): void
let width = 0
if format_ch == '*'
width = va_arg_int64(va_list)
else:
else
width = 0
for format_ch in format_range
if isdigit(format_ch) == 0
Expand Down
1 change: 1 addition & 0 deletions skel/include/mlalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#ifndef ML_ALLOC_H
#define ML_ALLOC_H
#define ML_ALLOC_GC

#ifndef ML_ALLOC_GC
GarbageCollector ml_gc;
Expand Down
18 changes: 18 additions & 0 deletions src/Def.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class Register(enum.Enum):
id_max = 14


# Context is provided as an extension due to the maturity of the project
# Scope: context_map.get(full_name).scope
# Small name: context_map.get(full_name).name
@dataclass
class Context:
name: str
scope: str


class VariableMetaKind(enum.Enum):
"""
Defines all possible structural types.
Expand Down Expand Up @@ -479,6 +488,14 @@ def global_modf_of(kind: VariableKind) -> str:

return modf_map[kind]

def curr_scope() -> str:
return module_name_list + fun_name_list

def name_of(full_name: str) -> str:
if full_name not in context_map:
print_error('name_of', f'No such small name for {full_name}')

return context_map.get(full_name).name

def full_name_of_fun(name: str, force_global: bool = False, exhaustive_match: bool = True):
# Namespace match
Expand Down Expand Up @@ -1498,6 +1515,7 @@ def get_counter():
counter = 0
var_off = 0
block_cnt = 0
context_map: Dict[str, Context] = dict()
macro_map: Dict[str, Macro] = dict()
var_map: Dict[str, Variable] = dict()
fun_map: Dict[str, Function] = dict()
Expand Down
Loading

0 comments on commit 4b077ac

Please sign in to comment.