add native function `ceil`

This commit is contained in:
ValKmjolnir 2023-10-26 00:04:20 +08:00
parent d56e1bff2c
commit bbed29eb65
4 changed files with 59 additions and 33 deletions

View File

@ -157,6 +157,11 @@ var builtin_floor(context* ctx, gc* ngc) {
return var::num(std::floor(value.num()));
}
var builtin_ceil(context* ctx, gc* ngc) {
auto value = ctx->localr[1];
return var::num(std::ceil(value.num()));
}
var builtin_num(context* ctx, gc* ngc) {
auto val = ctx->localr[1];
if (val.type==vm_num) {
@ -653,6 +658,7 @@ nasal_builtin_table builtin[] = {
{"__id", builtin_id},
{"__int", builtin_int},
{"__floor", builtin_floor},
{"__ceil", builtin_ceil},
{"__num", builtin_num},
{"__pop", builtin_pop},
{"__str", builtin_str},

View File

@ -42,6 +42,7 @@ var builtin_rand(context*, gc*);
var builtin_id(context*, gc*);
var builtin_int(context*, gc*);
var builtin_floor(context*, gc*);
var builtin_ceil(context*, gc*);
var builtin_num(context*, gc*);
var builtin_pop(context*, gc*);
var builtin_str(context*, gc*);

View File

@ -2,8 +2,16 @@
# 2021 ValKmjolnir
var (
_j_eof, _j_lbrace, _j_rbrace, _j_lbrkt, _j_rbrkt,
_j_comma, _j_colon, _j_str, _j_num, _j_id
_j_eof,
_j_lbrace,
_j_rbrace,
_j_lbrkt,
_j_rbrkt,
_j_comma,
_j_colon,
_j_str,
_j_num,
_j_id
) = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
var _j_content = [
@ -146,8 +154,9 @@ var parse = func() {
}
var match = func(type) {
if(token.type!=type)
if(token.type!=type) {
println("json::parse: line ",line,": expect ",_j_content[type]," but get `",token.content,"`.");
}
next();
return;
}

View File

@ -75,6 +75,10 @@ var floor = func(val) {
return __floor(val);
}
var ceil = func(val) {
return __ceil(val);
}
# exit using std::exit
var exit = func(val = -1) {
return __exit(val);
@ -236,11 +240,13 @@ var sort = func(){
(vec[left], vec[base]) = (vec[base], vec[left]);
var (i, j, tmp) = (left, right, vec[left]);
while(i<j) {
while(i<j and cmp(tmp,vec[j]))
while(i<j and cmp(tmp,vec[j])) {
j -= 1;
}
vec[i] = vec[j];
while(i<j and cmp(vec[i],tmp))
while(i<j and cmp(vec[i],tmp)) {
i += 1;
}
vec[j] = vec[i];
j -= 1;
}
@ -294,24 +300,28 @@ var ghosttype = func(ghost_object) {
# get the index of val in the vec
var vecindex = func(vec, val) {
forindex(var i;vec)
if(val==vec[i])
forindex(var i; vec) {
if(val==vec[i]) {
return i;
}
}
return nil;
}
# check if the object is an instance of the class
var isa = func(object, class) {
if (!ishash(object)) {
return 0;
return false;
}
if(!contains(object, "parents") or !isvec(object.parents)) {
return 0;
return false;
}
foreach(var elem;object.parents)
if(elem==class or isa(elem, class))
return 1;
return 0;
foreach(var elem; object.parents) {
if(elem==class or isa(elem, class)) {
return true;
}
}
return false;
}
# assert aborts when condition is not true