Carp/core/Color.carp
Scott Olsen 856171ef16
Support defining types in modules (BREAKING) (#1084)
* fix: don't set the inner env to globals in type mods

Previously, we set the inner environment of a type generated module to
the global env in cases where the overarching context didn't have an
inner env. This leads to problems where by the recognition of modules is
inconsistent, and one can't use the names of types as submodules in
certain circumstances.

This commit fixes that issue.

* refactor: refactor primitiveDefmodule

This refactor fixes a issues with meta information on submodules, for
instance, sigs on submodule functions used to result in a compiler error
about ambiguous identifiers. This fixes that.

Unfortunately, I don't have a precise idea about what exactly was wrong
with the original definition of this function. My suspicion is that the
recursion originally altered submodule paths in the wrong way, but I'm
not certain. In any case it's fixed.

* fix: ensure macros are expanded in the correct module

Previously, macro expansions folded over all forms after the top level
form, without performing any context updates on encountered
`defmodules`. This created an issue in which macro calls that produced
new bindings, "meta stubs", were *hoisted* out of submodules and into
the top-level module, creating duplicate definitions.

This commit fixes that issue by adding a special case for defmodule in
macroExpand.

* fix: ensure submodules and globals don't conflict

Previously, our module lookups during new module definition always
eventually fell back to the global environment, which caused submodules
that happen to share a name with a global module to be confused with the
global module. This change fixes that, so now one can define both
`Dynamic` (global) and `Foo.Dynamic` without issue.

* fix: remove old prefixes from vector tests

Commit 7b7cb5d1e replaced /= with a generic function. However, the
vector tests still called the specific Vector variants of this function,
which were removed when the generic was introduced. After recent
changes, these calls are now (correctly) identified as erroneous. My
guess is that they only worked in the past because of problems with our
lookups.

* chore: format code

* feat!: support defining types in modules

This commit adds support for defining types (using deftype) in modules.
Previously, all types were hoisted to the top level of the type
environment. After this commit, the type environment supports defining
nested modules just like the value env, so, calling the following:

```
(defmodule Foo (deftype Bar Baz))
```

Adds the following to the type env:

```
Foo : Module = {
    Bar : Type
}
```

and the following to the value env:

```
Foo : Module = {
    Bar : Module = {
        Baz : (Fn [] Foo.Bar)
        copy : (Fn [(Ref Foo.Bar q)] Foo.Bar)
        delete : (Fn [Foo.Bar] ())
        get-tag : (Fn [(Ref Foo.Bar q)] Int)
        prn : (Fn [(Ref Foo.Bar q)] String)
        str : (Fn [(Ref Foo.Bar q)] String)
    }
}

```

Such a type is *distinct* from any type defined at the top level that
happens to also have the name `Bar`.

This commit also updates info and tests to account for types in modules.

BREAKING CHANGE: This change is breaking since it alters the names of
types that were previously defined in modules. A good example of this is
the `Id` type in the `Color` module. Previously, one could refer to this
type by simply typing `Id` since it was hoisted to the top level. Now it
*must* be referred to by `Color.Id` since `Id` at the top level of the
type env and `Color.Id` (Id in the color module) are considered to be
distinct types.

* chore: format code

* refactor: use concat instead of intercalate

* chore: remove excess parentheses

* chore: Add todo to return IO () in printIfFound
2020-12-22 13:27:57 +01:00

81 lines
1.7 KiB
Plaintext

(defmodule Color
(hidden table)
(deftype Id
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
Reset
None
Bold
Italic
Underline
BlinkSlow
BlinkRapid
BgBlack
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite)
(use Id)
(defn hash [k]
(get-tag k))
(implements hash Color.hash)
(defn = [a b]
(= (hash (the (Ref Color.Id) a)) (hash b)))
(implements = Color.=)
(def table
{(Black) @"30"
(Red) @"31"
(Green) @"32"
(Yellow) @"33"
(Blue) @"34"
(Magenta) @"35"
(Cyan) @"36"
(White) @"37"
(Reset) @"0"
(None) @"0"
(Bold) @"1"
(Italic) @"3"
(Underline) @"4"
(BlinkSlow) @"5"
(BlinkRapid) @"6"
(BgBlack) @"40"
(BgRed) @"41"
(BgGreen) @"42"
(BgYellow) @"43"
(BgBlue) @"44"
(BgMagenta) @"45"
(BgCyan) @"46"
(BgWhite) @"47"})
(doc color "generates ANSI coloration based on a color name `cname`.")
(defn color [cid]
(let [n (Map.get &table &cid)]
(String.append "\x1b[" &(String.append &n "m"))))
(doc colorize "wraps a string `s` in ANSI coloration based on a color id `cid` and prints it.
It will reset the color afterwards.")
(defn colorize [cid s]
(String.append &(color cid) &(String.append s &(color (Reset)))))
)
(defmodule IO
(doc color "sets the output color using ANSI coloration based on a color id `cid`.")
(defn color [cid]
(print &(Color.color cid)))
(doc colorize "wraps a string in ANSI coloration based on a color id `cid` and prints it.")
(defn colorize [cid s]
(print &(Color.colorize cid s)))
)