corth-lang / Corth

A self-hosted stack based language like Forth
MIT License
8 stars 1 forks source link

Namespaces #30

Closed HuseyinSimsek7904 closed 8 months ago

HuseyinSimsek7904 commented 10 months ago

Currently, many of files contain macro and procedure names like namespace_name.procedure_name. This usually makes it easy to work with them, however this also causes inconsistencies where instead of ., - is used just like in the case of MODE-PEEKED. To get rid of this inconsistency, namespace keyword which prepends every definition inside with the namespace name and two colons can be implemented.

:: is chosen totally random and anything else could be chosen; for example :, -> or ..

//// Edited from std/collections/list64.corth

namespace list64  //// Any definition after this will be prepended with 'list64::'

macro init  //// This will be defined as 'list64::init'.
  // int: start-size -> ptr: dynamic-object
  // Start size is the capacity the dynamic object has when the object is created.
  // Save the dynamic object to a variable and use that variable address as the list object.
  inc 8 * malloc let _dynamic_ in
    _dynamic_ isn-null if
      0 _dynamic_ !64
    end
  _dynamic_ end
endmacro

macro get-dynamic  //// This will be defined as 'list64::get-dynamic'.
  // ptr: list64 -> ptr: dynamic
  @64
endmacro

macro array-addr  //// This will be defined as 'list64::array-addr'.
  // ptr: list64 -> ptr: array
  list::get-dynamic 8 +
endmacro

macro get-addr  //// This will be defined as 'list64::get-addr'.
  // int: index ptr: list64 -> ptr: address
  list64::array-addr array64::get-addr  //// Please assume that 'array64::get-addr' was defined before.
endmacro

macro get  //// This will be defined as 'list64::get'.
  // int: index ptr: list64 -> int: value
  // NOTE: This macro assumes that the index is valid.
  list64::get-addr @64
endmacro

end

Allowing nested namespaces may also have different use cases. For example, nesting namespace str under set might prepend the definitions with set::str::.

HuseyinSimsek7904 commented 8 months ago

here is a link of the branch for the development of this feature.