Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
add support for is operator to check variable type
Browse files Browse the repository at this point in the history
  • Loading branch information
Thecarisma committed May 19, 2019
1 parent 516af14 commit 34d4a9e
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 38 deletions.
3 changes: 2 additions & 1 deletion ROADMAP.MD
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 110,7 @@ build, installation, features e.t.c
- [ ] change the method **execCode** to **eval**
- [x] use the Error method in classes to attend to error that occur when class access is in braces
- [ ] support for string interpolation
- [ ] add the **is** operator in place of the InstanceOf function
- [x] add the **is** operator in place of the InstanceOf function
- [ ] change the dynamic module to use extension .dysim t reduce platform extensions overload

### simplify
Expand All @@ -137,6 137,7 @@ To dos and features relative to the simple-lang standard modules which includes
issue related to the module

- [ ] simple.core.*
- [ ] simple.core.Object : the super type for all class objects
- [ ] simple.core.Boolean for primitive `true` and `false` to Object
- [ ] change all existing module methods and function identifier to starts with Capital letter instead of small letter
- [ ] make the conf.sim file in dynamic module folder for compiling dynamic module instead of makefiles
Expand Down
20 changes: 7 additions & 13 deletions examples/basic/test.sim
Original file line number Diff line number Diff line change
@@ -1,16 1,10 @@
Onene()

Test()
Test()
One()

block Test()
var one = "hello World"
@one

block One()
var One = [1,2,3,4]
block Onene()
var Onet = [1,2,3,4]
var on
for on in One
@"Done with : " on
for on in Onet
display "Done with : " on nl

end
One()
Onene()
17 changes: 17 additions & 0 deletions examples/tests/ExtendedTests/OperatorTest.sim
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@

from simple.core.Object
var obj = new Object()

if obj is "Object" @"yes it is an Object" end
if "1" is "String" @"yes '1' is a String" end
display [1,2,3,4,6] is "List" display nl
display 1 is "Number" display nl
display "HelloWorld" is "String" display nl

@""
@""
if obj !is "String" @"yes it is not String" end
if "1" !is "String" @"yes '1' is a String" end
display [1,2,3,4,6] !is "String" display nl
display 1 !is "Number" display nl
display "HelloWorld" !is "String" display nl
5 changes: 5 additions & 0 deletions examples/tests/ManualModuleTests/simple/core/ObjectTest.sim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,11 @@ from simple.core.Object

var obj = new Object()
@obj.ToString()
@obj.Name()
@obj.Module()
@obj.AbsoluteName()
@obj.Equals(obj)
@obj is obj

class ObjectTest : Object

Expand Down
20 changes: 10 additions & 10 deletions modules/simple/core/Object.sim
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 60,9 @@ class Object
*/
block AbsoluteName()
if Module() == ""
return Name
return Name()
end
return Module "." Name
return Module "." Name()

/*

Expand All @@ -74,27 74,27 @@ class Object

*/
block Hashcode()
if hashcode == 0
hashcode = __object_address(self)
if Hashcode == 0
Hashcode = __object_address(self)
end
return hashcode
return Hashcode

/*

*/
block Equals(obj:duck)
return obj is Object
return obj is "Object" and Hashcode == obj.Hashcode

/*

*/
block Operator(op:string, obj:duck)
switch op
case "=="
return Equals(value)
return Equals(obj)
case "!="
return !Equals(value)
case "getType"
return !Equals(obj)
case "GetType"
return Absolutename()
default
Throw(self,"This Object does not define a behaviour for the operator : " op)
Expand All @@ -110,7 110,7 @@ class Object
/*

*/
var hashcode = 0
var Hashcode = 0

/*

Expand Down
16 changes: 8 additions & 8 deletions modules/simple/core/String.sim
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 29,13 @@ CallModule("libstring_savant")
*/
module simple.core

__Empty = Ascii(0)
STRING_EMPTY = "The string provided is empty"
STRING_COMPARE_ERROR = "Array index out of bound : The length of the comparing string is greater than the main string "
STRING_LESS = "Array index out of bound : The string is empty"
BACKSPACE = Ascii(8)
END_OF_LINE = Ascii(13)
LINE_BREAK = Ascii(10)
final var __Empty = Ascii(0)
final var STRING_EMPTY = "The string provided is empty"
final var STRING_COMPARE_ERROR = "Array index out of bound : The length of the comparing string is greater than the main string "
final var STRING_LESS = "Array index out of bound : The string is empty"
final var BACKSPACE = Ascii(8)
final var END_OF_LINE = Ascii(13)
final var LINE_BREAK = Ascii(10)

/*

Expand Down Expand Up @@ -574,7 574,7 @@ block StrEndsWith(object, check)
if check == "" {
return false
}
var length = LengthOf(check) - 1 @"Length:" length
var length = LengthOf(check) - 1
var right = __right(object,length)
return check == right

Expand Down
2 changes: 2 additions & 0 deletions simple/include/simple_codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 47,8 @@ typedef enum IC_OPERATIONS {
ICO_LOADINDEXADDRESS ,
ICO_LOADAPUSHV ,
/* Comparsion operators */
ICO_IS ,
ICO_ISNOT ,
ICO_EQUAL ,
ICO_LESS ,
ICO_GREATER ,
Expand Down
1 change: 1 addition & 0 deletions simple/include/simple_scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 90,7 @@ typedef enum SCANNER_KEYWORD {
KEYWORD_ELSEIF ,
KEYWORD_CASE ,
KEYWORD_VAR ,
KEYWORD_IS ,
KEYWORD_CHANGESIMPLEKEYWORD ,
KEYWORD_CHANGESIMPLEIOPERATOR ,
KEYWORD_LOADSYNTAX
Expand Down
4 changes: 4 additions & 0 deletions simple/include/simple_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 205,10 @@ SIMPLE_API void simple_vm_loadaddressfirst ( VM *vm ) ;
SIMPLE_API void simple_vm_endblockexec ( VM *vm ) ;
/* Compare */

SIMPLE_API void simple_vm_is ( VM *vm ) ;

SIMPLE_API void simple_vm_isnot ( VM *vm ) ;

SIMPLE_API void simple_vm_equal ( VM *vm ) ;

SIMPLE_API void simple_vm_lessequal ( VM *vm ) ;
Expand Down
4 changes: 2 additions & 2 deletions simple/sources/simple_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 434,7 @@ void simple_vmlib_len ( void *pointer )
else {
SIMPLE_VM_STACK_PUSHPVALUE(SIMPLE_API_GETPOINTER(1));
SIMPLE_VM_STACK_OBJTYPE = SIMPLE_API_GETPOINTERTYPE(1) ;
simple_vm_expr_npoo(vm,"lengthOf",0);
simple_vm_expr_npoo(vm,"LengthOf",0);
vm->nIgnoreNULL = 1 ;
}
} else {
Expand Down Expand Up @@ -547,7 547,7 @@ void simple_vmlib_type ( void *pointer )
else if ( SIMPLE_API_ISOBJECT(1) ) {
SIMPLE_VM_STACK_PUSHPVALUE(SIMPLE_API_GETPOINTER(1));
SIMPLE_VM_STACK_OBJTYPE = SIMPLE_API_GETPOINTERTYPE(1) ;
simple_vm_expr_npoo(vm,"getType",0);
simple_vm_expr_npoo(vm,"GetType",0);
vm->nIgnoreNULL = 1 ;
}
else if ( SIMPLE_API_ISLIST(1) ) {
Expand Down
48 changes: 45 additions & 3 deletions simple/sources/simple_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 134,7 @@ int simple_parser_equalornot ( Parser *parser )

puts("Rule : EqualOrNot --> Compare");
#endif
while ( simple_parser_isoperator2(parser,OP_EQUAL) || simple_parser_isoperator2(parser,OP_NOT) ) {
while ( simple_parser_isoperator2(parser,OP_EQUAL) || simple_parser_isoperator2(parser,OP_NOT) || simple_parser_iskeyword(parser,KEYWORD_IS) ) {
if ( simple_parser_isoperator2(parser,OP_NOT) ) {
simple_parser_nexttoken(parser);
SIMPLE_PARSER_IGNORENEWLINE ;
Expand All @@ -157,12 157,54 @@ int simple_parser_equalornot ( Parser *parser )
}
#endif
}
else {
else if (simple_parser_iskeyword(parser,KEYWORD_IS))
{
simple_parser_nexttoken(parser); SIMPLE_PARSER_IGNORENEWLINE ;
x = simple_parser_compare(parser);
if ( x == 0 ) {
return 0 ;
}
/* Generate Code */
simple_parser_icg_newoperation(parser,ICO_ISNOT);
/* Generate Location for nPC for Operator Overloading */
simple_parser_icg_newoperandint(parser,0);
#if SIMPLE_PARSERTRACE
SIMPLE_STATE_CHECKPRINTRULES

{
puts("Rule : Is --> Compare");
puts("Rule : Is --> Is '!is' Is");
}
#endif
}
else
{
parser_error(parser,PARSER_ERROR_EXPROPERATOR);
return 0 ;
}
}
else {
else if (simple_parser_iskeyword(parser,KEYWORD_IS))
{
simple_parser_nexttoken(parser); SIMPLE_PARSER_IGNORENEWLINE ;
x = simple_parser_compare(parser);
if ( x == 0 ) {
return 0 ;
}
/* Generate Code */
simple_parser_icg_newoperation(parser,ICO_IS);
/* Generate Location for nPC for Operator Overloading */
simple_parser_icg_newoperandint(parser,0);
#if SIMPLE_PARSERTRACE
SIMPLE_STATE_CHECKPRINTRULES

{
puts("Rule : Is --> Compare");
puts("Rule : Is --> Is 'is' Is");
}
#endif
}
else
{
simple_parser_nexttoken(parser);
SIMPLE_PARSER_IGNORENEWLINE ;
if ( simple_parser_isoperator2(parser,OP_EQUAL) )
Expand Down
3 changes: 2 additions & 1 deletion simple/sources/simple_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,7 @@ const char * SIMPLE_KEYWORDS[] = {"if","to","or","and","not","for","new","block"

"in","continue","module","import","private","final","step","do","exec","elif",

"case", "var"/**, "changesimplekeyword","changesimpleoperator","loadsyntax"**/} ;
"case", "var", "is"/**, "changesimplekeyword","changesimpleoperator","loadsyntax"**/} ;

/* Secondary (Not Enforced) */
const char * SIMPLE_SECONDARY_KEYWORDS[] = {"..."} ;
Expand Down Expand Up @@ -557,6 557,7 @@ void simple_scanner_keywords ( Scanner *scanner )
simple_list_addstring_gc(scanner->sState,scanner->Keywords,"elif");
simple_list_addstring_gc(scanner->sState,scanner->Keywords,"case");
simple_list_addstring_gc(scanner->sState,scanner->Keywords,"var");
simple_list_addstring_gc(scanner->sState,scanner->Keywords,"is");
/*
** The next keywords are sensitive to the order and keywords count
** if you will add new keywords revise constants and simple_scanner_checktoken()
Expand Down
6 changes: 6 additions & 0 deletions simple/sources/simple_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 448,12 @@ SIMPLE_API void simple_vm_execute ( VM *vm )
case ICO_LESSEQUAL :
simple_vm_lessequal(vm);
break ;
case ICO_IS :
simple_vm_is(vm);
break ;
case ICO_ISNOT :
simple_vm_isnot(vm);
break ;
case ICO_EQUAL :
simple_vm_equal(vm);
break ;
Expand Down
Loading

0 comments on commit 34d4a9e

Please sign in to comment.