kitlang / kit

Kit: a magical, high performance programming language, designed for game development.
https://www.kitlang.org
Other
1.02k stars 29 forks source link

Running some files produces no errors but does not run or produce runnable main. #131

Closed AlexPoulsen closed 5 years ago

AlexPoulsen commented 5 years ago

Description:

Running some files produces no errors but does not run or produce runnable main.

macbookpro ~/s/src ><> kitc --run "/Users/macbookpro/string-builder/src/stringbuilder.kit"
[2019-05-12 15:35:47.3283] ===> parsing and building module graph
[2019-05-12 15:35:47.3791] ===> processing C includes
[2019-05-12 15:35:47.6403] ===> expanding macros
[2019-05-12 15:35:47.6410] ===> resolving module types
[2019-05-12 15:35:47.6564] ===> flattening trait implementations
[2019-05-12 15:35:47.6574] ===> typing module content
kitc: Prelude.head: empty list
kitc --run "/Users/macbookpro/string-builder/src/stringbuilder.kit" --verbose --verbose
...
[2019-05-12 15:38:11.4424] DBG: resolving types for ModuleUsing implicit Expr {expr = Identifier mallocator, pos = @/usr/local/lib/kit/prelude.kit:33:16-25}
[2019-05-12 15:38:11.4517] DBG: resolving types for TypeDeclaration (TypeDefinition {typeName = (["kit","either"],"Either"), typeBundle = Nothing, typeMonomorph = [], typePos = @/usr/local/lib/kit/kit/either.kit:1:1-11, typeMeta = [], typeModifiers = [], typeMethods = [], typeStaticFields = [], typeStaticMethods = [], typeRules = [], typeSubtype = Enum {enumVariants = [], enumUnderlyingType = Int}, typeParams = [TypeParam {paramName = "A", typeParamPos = @/usr/local/lib/kit/kit/either.kit:1:13, constraints = [], typeParamIsConstant = False, typeParamDefault = Nothing},TypeParam {paramName = "B", typeParamPos = @/usr/local/lib/kit/kit/either.kit:1:16, constraints = [], typeParamIsConstant = False, typeParamDefault = Nothing}]})
[2019-05-12 15:38:11.4556] DBG: resolving types for ExtendDefinition Either [DefVariant (EnumVariant {variantName = ([],"Left"), variantParent = kitc: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at src/Kit/Ast/Definitions/EnumVariant.hs:38:24 in kitlang-0.1.0-1asFTmIzBYCK4VFUEBcsTO:Kit.Ast.Definitions.EnumVariant
macbookpro ~/s/src ><> kitc -c -g "/Users/macbookpro/string-builder/src/stringbuilder.kit"
[2019-05-12 15:41:03.0982] ===> parsing and building module graph
[2019-05-12 15:41:03.1296] ===> processing C includes
[2019-05-12 15:41:03.3762] ===> expanding macros
[2019-05-12 15:41:03.3768] ===> resolving module types
[2019-05-12 15:41:03.3961] ===> flattening trait implementations
[2019-05-12 15:41:03.3976] ===> typing module content
kitc: Prelude.head: empty list
(does not produce main file that can be run with lldb)
kitc --run "/Users/macbookpro/string-builder/src/stringbuilder.kit" --verbose --verbose
...
[2019-05-12 15:42:01.7540] DBG: resolving types for ModuleUsing implicit Expr {expr = Identifier mallocator, pos = @/usr/local/lib/kit/prelude.kit:33:16-25}
[2019-05-12 15:42:01.7556] DBG: resolving types for TypeDeclaration (TypeDefinition {typeName = (["kit","either"],"Either"), typeBundle = Nothing, typeMonomorph = [], typePos = @/usr/local/lib/kit/kit/either.kit:1:1-11, typeMeta = [], typeModifiers = [], typeMethods = [], typeStaticFields = [], typeStaticMethods = [], typeRules = [], typeSubtype = Enum {enumVariants = [], enumUnderlyingType = Int}, typeParams = [TypeParam {paramName = "A", typeParamPos = @/usr/local/lib/kit/kit/either.kit:1:13, constraints = [], typeParamIsConstant = False, typeParamDefault = Nothing},TypeParam {paramName = "B", typeParamPos = @/usr/local/lib/kit/kit/either.kit:1:16, constraints = [], typeParamIsConstant = False, typeParamDefault = Nothing}]})
[2019-05-12 15:42:01.7705] DBG: resolving types for ExtendDefinition Either [DefVariant (EnumVariant {variantName = ([],"Left"), variantParent = kitc: Prelude.undefined
CallStack (from HasCallStack):
  error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
  undefined, called at src/Kit/Ast/Definitions/EnumVariant.hs:38:24 in kitlang-0.1.0-1asFTmIzBYCK4VFUEBcsTO:Kit.Ast.Definitions.EnumVariant
(does not produce main file that can be run with lldb)

Code example:

/**
 * An StringBuilder implementation in kit.
 * @author Tyler Bezera
 * @author Alex Poulsen
 */

struct StringBuilder {
    private var chars: CString;
    private var length: Int;
    private var allocated: Int;

    public static function new() {
        return struct Self {
            chars: "",
            length: 10
        };
    }

    public static function get_len() {
        printf("%d", length);
    }

    function ensure_space(add_len: Int)
    {
        if add_len == 0 {
            return;
        }

        if (this.allocated >= this.length + add_len + 1) {
            return;
        }

        while (this.allocated < this.len + add_len + 1) {
            /* Doubling growth strategy. */
            this.allocated <<= 1;
            if (this.allocated == 0) {
                /* Left shift of max bits will go to 0. An unsigned type set to
                * -1 will return the maximum possible size. However, we should
                *  have run out of memory well before we need to do this. Since
                *  this is the theoretical maximum total system memory we don't
                *  have a flag saying we can't grow any more because it should
                *  be impossible to get to this point. */
                this.allocated--;
            }
        }
        this.chars = realloc(this.chars, this.allocated);
        this.length = this.length + add_len;
    }
}

function main() {
    var builder = StringBuilder.new();
    builder.get_len();
    builder.ensure_space(8);
    builder.get_len();
    builder.ensure_space(8);
    builder.get_len();
}

Environment:

macbookpro ~/s/src ><> kitc --env                                                                                                                                                       15:42:01
[2019-05-12 15:42:28.0476] ===> kitc version
[2019-05-12 15:42:28.0493] DBG: 0.1.0
[2019-05-12 15:42:28.0498] ===> OS
[2019-05-12 15:42:28.0503] DBG: darwin
[2019-05-12 15:42:28.0507] ===> Source paths
[2019-05-12 15:42:28.0512] DBG: ["src","/usr/local/lib/kit"]
[2019-05-12 15:42:28.0517] ===> Standard prelude location
[2019-05-12 15:42:28.0523] DBG: /usr/local/lib/kit/prelude.kit
[2019-05-12 15:42:28.0532] ===> ** COMPILER **
[2019-05-12 15:42:28.0537] ===> Toolchain
[2019-05-12 15:42:28.0541] DBG: /Users/macbookpro/kit/toolchains/darwin-gcc
[2019-05-12 15:42:28.0547] ===> Compiler
[2019-05-12 15:42:28.0551] DBG: gcc
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
[2019-05-12 15:42:28.1072] ===> Include paths
[2019-05-12 15:42:28.1077] DBG: []
[2019-05-12 15:42:28.1082] ===> Compiler flags
[2019-05-12 15:42:28.1088] DBG: ["-U__BLOCKS__","-std=c99","-pedantic","-O3","-Os","-Wno-unused-command-line-argument","-Wno-missing-braces","-Wno-shift-op-parentheses","-Wno-expansion-to-defined","-Wno-gnu-zero-variadic-macro-arguments"]
[2019-05-12 15:42:28.1160] ===> Linker flags
[2019-05-12 15:42:28.1168] DBG: ["-std=c99","-pedantic","-O3","-Os","-Wno-unused-command-line-argument","-Wno-missing-braces","-Wno-shift-op-parentheses","-Wno-expansion-to-defined","-Wno-gnu-zero-variadic-macro-arguments"]
bendmorris commented 5 years ago

This is the problem:

    public static function get_len() {
        // ...
    }

    var builder = StringBuilder.new();
    builder.get_len();

Static methods would be called like StringBuffer.get_len(), not on a value. You probably didn't want a static method.

I just pushed a fix so this will throw an error instead of dying.