diff --git a/build/supertiny.flx b/build/supertiny.flx index 37b1b232..b46fdc7c 100644 --- a/build/supertiny.flx +++ b/build/supertiny.flx @@ -7,11 +7,19 @@ export supertiny import "libc" as _ // import "stdio" as _ + + +ffi fn rofl() as "lmao" +ffi fn rofl(x: int) -> void as "haha" + @entry fn main() { - printf("!\n") + rofl() + rofl(20) } + + /* ! more things. ! caa 03/10 diff --git a/changelog.md b/changelog.md index 4b45c6de..75c5baa4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,10 @@ ## CHANGELOG (FIXED / IMPLEMENTED THINGS) `(??)` +- flip the declaration of `ffi-as` so the external name is after `as` -- and now as a string literal (because your external names might have + flax-illegal characters inside) + +`(a2721fc)` - declare foreign functions `as` something -- useful for OpenGL especially, (1) to deduplicate the `gl` prefix, and (2) to allow overloading of all the different `glVertex` where `N = 2, 3, 4`, `T = i, f, d` or something. diff --git a/source/frontend/parser/function.cpp b/source/frontend/parser/function.cpp index f4e20ab6..fb923fe1 100644 --- a/source/frontend/parser/function.cpp +++ b/source/frontend/parser/function.cpp @@ -169,14 +169,14 @@ namespace parser if(st.front() == TT::As) { st.pop(); - if(st.front() != TT::Identifier) - expectedAfter(st.loc(), "identifier", "'as' in foreign function declaration", st.front().str()); + if(st.front() != TT::StringLiteral) + expectedAfter(st.loc(), "string literal", "'as' in foreign function declaration", st.front().str()); - ffn->asName = st.eat().str(); + ffn->realName = st.eat().str(); } else { - ffn->asName = ffn->name; + ffn->realName = ffn->name; } return ffn; diff --git a/source/include/ast.h b/source/include/ast.h index 8beaaf76..bd53bb97 100644 --- a/source/include/ast.h +++ b/source/include/ast.h @@ -190,7 +190,7 @@ namespace ast using Arg = FuncDefn::Arg; std::string name; - std::string asName; + std::string realName; std::vector args; pts::Type* returnType = 0; diff --git a/source/main.cpp b/source/main.cpp index 081847fe..e7341e53 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -23,6 +23,26 @@ struct timer }; +#ifdef _WIN32 +#define DLLEXPORT __declspec(dllexport) +#else +#define DLLEXPORT +#endif + +extern "C" DLLEXPORT void lmao() +{ + printf("LMAO!\n"); +} + +extern "C" DLLEXPORT void haha(int x) +{ + for(int i = 0; i < x; i++) + printf("HA"); + + printf("!\n"); +} + + static void compile(std::string in, std::string out) { auto ts = std::chrono::high_resolution_clock::now(); diff --git a/source/typecheck/function.cpp b/source/typecheck/function.cpp index 9f6c7685..fa9961b5 100644 --- a/source/typecheck/function.cpp +++ b/source/typecheck/function.cpp @@ -189,8 +189,7 @@ TCResult ast::ForeignFuncDefn::typecheck(sst::TypecheckState* fs, fir::Type* inf auto retty = fs->convertParserTypeToFIR(this->returnType); - // use our 'asname' as the identifier. - defn->id = Identifier(this->asName, IdKind::Name); + defn->id = Identifier(this->name, IdKind::Name); defn->params = ps; defn->returnType = retty; @@ -198,7 +197,7 @@ TCResult ast::ForeignFuncDefn::typecheck(sst::TypecheckState* fs, fir::Type* inf defn->isVarArg = this->isVarArg; // the realname is the actual name of the function. - defn->realName = this->name; + defn->realName = this->realName; if(this->isVarArg) defn->type = fir::FunctionType::getCVariadicFunc(util::map(ps, [](FnParam p) -> auto { return p.type; }), retty); @@ -234,8 +233,7 @@ TCResult ast::ForeignFuncDefn::typecheck(sst::TypecheckState* fs, fir::Type* inf this->generatedDecl = defn; - // same, use our 'asname'. - fs->stree->addDefinition(this->asName, defn); + fs->stree->addDefinition(this->name, defn); return TCResult(defn); }