Skip to content

Commit

Permalink
add typeof command @TheLoneWolfling
Browse files Browse the repository at this point in the history
  • Loading branch information
James Edwards committed Jul 17, 2016
1 parent 1cb5e93 commit e9e2a7c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/apply_single_value_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct Value apply_single_value_func(struct Value *kwd_val, struct Execution_bun
exec_bundle->ast = read_import(value_as_string(value));
*value = execute(exec_bundle);
exec_bundle->ast = old_ast;
} else if(string_concat_heap(&kwd, &typeof_const)){
*value = value_from_string(type_to_string(value->type));
} else {
value->type = UNDEF;
value->data.bl = false;
Expand Down
9 changes: 9 additions & 0 deletions src/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,17 @@ struct String false_const = STR_NEW("false");

struct String true_const = STR_NEW("true");

struct String typeof_const = STR_NEW("typeof");

struct String function_names = STR_NEW("[if, read, let, set, each, for, do, switch, parallel, import, map, reduce, substring, while]");

struct String keyword_const = STR_NEW("keyword");
struct String number_const = STR_NEW("number");
struct String string_const = STR_NEW("string");
struct String boolean_const = STR_NEW("boolean");
struct String array_const = STR_NEW("array");
struct String undefined_const = STR_NEW("undefined");

const char open_parens_const = '(';
const char close_parens_const = ')';
const char root_type_const = 'r';
Expand Down
8 changes: 8 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ extern struct String read_line_const;
extern struct String function_names;
extern struct String false_const;
extern struct String true_const;
extern struct String typeof_const;

extern struct String keyword_const;
extern struct String number_const;
extern struct String string_const;
extern struct String boolean_const;
extern struct String array_const;
extern struct String undefined_const;

extern const char open_parens_const;
extern const char close_parens_const;
Expand Down
2 changes: 1 addition & 1 deletion src/core_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ bool can_cast_to_size_t(huo_int_t i) {
struct Value set(struct Value index_val, struct Value item, struct Value *to_set) {
huo_int_t index = value_as_long(&index_val);
if (!can_cast_to_size_t(index)) {
ERROR("Index out of range for set: should be 0 <= %" PRIhi " < %zu", index, SIZE_MAX);
ERROR("Index out of range for set: should be 0 <= %" PRIhi " < %llu", index, SIZE_MAX);

This comment has been minimized.

Copy link
@TheLoneWolfling

TheLoneWolfling Jul 18, 2016

Collaborator

This is wrong. SIZE_MAX is (or should be) of type size_t, as SIZE_MAX is defined to be the maximum value that a size_t can hold.

This comment has been minimized.

Copy link
@incrediblesound

incrediblesound Jul 18, 2016

Member

I dont know why but I had to change this or else it wouldn't compile.

This comment has been minimized.

Copy link
@TheLoneWolfling

TheLoneWolfling Jul 18, 2016

Collaborator

What was the error message? Perhaps your compiler isn't using the "right" type for SIZE_MAX.

Try this instead:

ERROR("Index out of range for set: should be 0 <= %" PRIhi " < %zu", index, (size_t) SIZE_MAX);

i.e. with an explicit cast.

This comment has been minimized.

Copy link
@incrediblesound

incrediblesound Jul 18, 2016

Member

I will try that.

This comment has been minimized.

Copy link
@TheLoneWolfling

TheLoneWolfling Jul 18, 2016

Collaborator

If it works, also add a comment...

}
if (to_set->type == ARRAY) {
return value_from_array(array_set((size_t) index, item, value_as_array(to_set)));
Expand Down
27 changes: 27 additions & 0 deletions src/structures/value.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include "structures.h"
#include "../constants.h"
#include "value.h"

#include "../base_util.h"
Expand Down Expand Up @@ -225,6 +226,32 @@ void value_negate(struct Value *v) {
}
}

struct String type_to_string(enum Value_type type){
switch (type) {
case STRING:
return string_copy_stack(&string_const);
break;
case FLOAT:
case LONG:
return string_copy_stack(&number_const);
break;
case ARRAY:
return string_copy_stack(&array_const);
break;
case BOOL:
return string_copy_stack(&boolean_const);

This comment has been minimized.

Copy link
@incrediblesound

incrediblesound Jul 17, 2016

Member

Is the string copy stack redundant here?

This comment has been minimized.

Copy link
@TheLoneWolfling

TheLoneWolfling Jul 18, 2016

Collaborator

Not currently. (Consider something like (return [(let a "a") (typeof a) (let a 2)])...)

But if we decide to make strings immutable internally - or rather, if you do - then yes.

break;
case KEYWORD:
return string_copy_stack(&keyword_const);
break;
case UNDEF:
return string_copy_stack(&undefined_const);
break;
default:
ERROR("Type error: unrecognized type: '%c'.", type)
}
}

void value_free_stack(struct Value v) {
switch (v.type) {
case ARRAY:
Expand Down
1 change: 1 addition & 0 deletions src/structures/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ size_t length(struct Value a);
unsigned long value_keyword_hash_code(void *value);
bool value_keyword_equality(void *a, void *b);

struct String type_to_string(enum Value_type type);
bool value_equals_shallow(struct Value *a, struct Value *b);
void value_negate(struct Value *v);
void value_free(struct Value *v);
Expand Down

2 comments on commit e9e2a7c

@incrediblesound
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheLoneWolfling I tried to use your utility functions to do this here but I'm not sure if I used them well.

@TheLoneWolfling
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Please sign in to comment.