Skip to content

Commit

Permalink
Implemented return type constraints. THERE ARE PROBABLY BUGS. Also th…
Browse files Browse the repository at this point in the history
…e doc comments haven't yet been modified to accommodate them, so I'm not marking the issue as closed yet.
  • Loading branch information
JarrettBillingsley committed Nov 8, 2014
1 parent 02c5166 commit ad4395a
Show file tree
Hide file tree
Showing 17 changed files with 548 additions and 91 deletions.
1 change: 1 addition & 0 deletions crocgrammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ Parameter:
ReturnTypes:
: List(Type) (, ...)?
: ...
: void

Type:
BasicType
Expand Down
33 changes: 33 additions & 0 deletions src/croc/api/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,39 @@ extern "C"
return f->isVararg();
}

/** \returns the number of \a non-variadic values that the function at \c func returns. */
uword_t croc_function_getNumReturns(CrocThread* t_, word_t func)
{
auto t = Thread::from(t_);
API_CHECK_PARAM(f, func, Function, "func");

if(f->isNative)
return 0;
else
return f->scriptFunc->numReturns;
}

/** \returns the maximum number of values that the function at \c func can return. For variadic return functions,
this will be an absurdly large number. */
uword_t croc_function_getMaxReturns(CrocThread* t_, word_t func)
{
auto t = Thread::from(t_);
API_CHECK_PARAM(f, func, Function, "func");

if(f->isNative || f->scriptFunc->isVarret)
return cast(uword_t)-1;
else
return f->scriptFunc->numReturns;
}

/** \returns nonzero if the function at \c func has variadic returns. */
int croc_function_isVarret(CrocThread* t_, word_t func)
{
auto t = Thread::from(t_);
API_CHECK_PARAM(f, func, Function, "func");
return f->isVarret();
}

/** \returns nonzero if the function at \c func is native. */
int croc_function_isNative(CrocThread* t_, word_t func)
{
Expand Down
3 changes: 3 additions & 0 deletions src/croc/apifuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ CROCAPI word_t croc_function_pushDef (CrocThread* t, word_t func);
CROCAPI uword_t croc_function_getNumParams (CrocThread* t, word_t func);
CROCAPI uword_t croc_function_getMaxParams (CrocThread* t, word_t func);
CROCAPI int croc_function_isVararg (CrocThread* t, word_t func);
CROCAPI uword_t croc_function_getNumReturns (CrocThread* t, word_t func);
CROCAPI uword_t croc_function_getMaxReturns (CrocThread* t, word_t func);
CROCAPI int croc_function_isVarret (CrocThread* t, word_t func);
CROCAPI int croc_function_isNative (CrocThread* t, word_t func);

/** Like \ref croc_function_newWithEnv, except it uses the current environment namespace, so the only values it expects
Expand Down
20 changes: 16 additions & 4 deletions src/croc/base/opcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ONE SHORT:
endfinal: rethrow any in-flight exception, or continue unwinding if doing so
ret: return
checkparams: check params against param masks
checkrets: check returns against return masks
(rd)
inc: rd++
dec: rd--
Expand All @@ -39,7 +40,9 @@ ONE SHORT:
close: close open upvals down to and including rd
objparamfail: give an error about parameter rd not being of an acceptable type
(rdimm)
unwind: unwind rdimm number of EH frames
unwind: unwind rdimm number of EH frames
objretfail: give an error about return rd not being of an acceptable type
retasfloat: convert return stack value rdimm to float
TWO SHORTS:
Expand Down Expand Up @@ -78,9 +81,11 @@ TWO SHORTS:
setu: upvals[uimm] = rd
newarr: rd = array.new(constTable[uimm])
namespacenp: rd = namespace constTable[uimm] : null {}
moveret: rd = returnStack[uimm]
(rdimm, rs)
throw: throw the value in rs; rd == 0 means normal throw, rd == 1 means rethrow
switch: switch on the value in rs using switch table index rd
throw: throw the value in rs; rd == 0 means normal throw, rd == 1 means rethrow
switch: switch on the value in rs using switch table index rd
customretfail: give error message about return rdimm not satisfying the constraint whose name is in rs
(rdimm, imm)
jmp: if rd == 1, jump by imm, otherwise no-op
Expand Down Expand Up @@ -111,6 +116,7 @@ THREE SHORTS:
namespace: rd = namespace constTable[uimm] : rt {}
(rdimm, rs, imm)
istrue: if the truth value of rs matches the truth value in rd, jump by imm
checkobjret: if(!isInstance(returnStack[rdimm]) || returnStack[rdimm] as rs) jump by imm
FOUR SHORTS:
Expand Down Expand Up @@ -159,6 +165,7 @@ FIVE SHORTS:
X(Inc),\
X(Dec),\
X(Move),\
X(MoveRet),\
X(NewGlobal),\
X(GetGlobal),\
X(SetGlobal),\
Expand Down Expand Up @@ -198,9 +205,13 @@ FIVE SHORTS:
X(VargSlice),\
X(Yield),\
X(CheckParams),\
X(CheckRets),\
X(CheckObjParam),\
X(CheckObjRet),\
X(ObjParamFail),\
X(ObjRetFail),\
X(CustomParamFail),\
X(CustomRetFail),\
X(AssertFail),\
X(Length),\
X(LengthAssign),\
Expand All @@ -226,7 +237,8 @@ FIVE SHORTS:
X(AsBool),\
X(AsInt),\
X(AsFloat),\
X(AsString)
X(AsString),\
X(RetAsFloat)

#define POOP(x) Op_ ## x
enum Op
Expand Down
Loading

0 comments on commit ad4395a

Please sign in to comment.