@@ -5,6 +5,7 @@ use super::thecodenode::TheCodeNodeData;
55#[ derive( Serialize , Deserialize , Clone , Debug , PartialEq ) ]
66pub enum TheCodeAtom {
77 Assignment ( String ) ,
8+ Comparison ( String ) ,
89 Value ( TheValue ) ,
910 Add ,
1011 Multiply ,
@@ -20,20 +21,20 @@ pub enum TheCodeAtom {
2021 Return ,
2122 EndOfExpression ,
2223 EndOfCode ,
23- Switch ,
24- CaseCondition ,
25- CaseBody ,
2624}
2725
2826impl TheCodeAtom {
2927 pub fn uneven_slot ( & self ) -> bool {
30- matches ! ( self , TheCodeAtom :: Assignment ( _name) )
28+ matches ! ( self , TheCodeAtom :: Assignment ( _) )
29+ || matches ! ( self , TheCodeAtom :: Comparison ( _) )
3130 || matches ! ( self , TheCodeAtom :: Add )
3231 || matches ! ( self , TheCodeAtom :: Multiply )
3332 }
3433
3534 pub fn can_assign ( & self ) -> bool {
36- matches ! ( self , TheCodeAtom :: LocalSet ( _name) )
35+ matches ! ( self , TheCodeAtom :: LocalSet ( _) )
36+ || matches ! ( self , TheCodeAtom :: ObjectSet ( _, _) )
37+ || matches ! ( self , TheCodeAtom :: Pulse )
3738 }
3839
3940 pub fn from_json ( json : & str ) -> Self {
@@ -46,6 +47,43 @@ impl TheCodeAtom {
4647
4748 pub fn to_node ( & self , ctx : & mut TheCompilerContext ) -> TheCodeNode {
4849 match self {
50+ TheCodeAtom :: Comparison ( _) => {
51+ let call: TheCodeNodeCall =
52+ |_stack : & mut Vec < TheValue > ,
53+ data : & mut TheCodeNodeData ,
54+ sandbox : & mut TheCodeSandbox | {
55+ if let Some ( f) = data. sub_functions . first_mut ( ) {
56+ if let Some ( v) = f. execute ( sandbox) . pop ( ) {
57+ if data. values [ 0 ] == v {
58+ println ! ( "yes" ) ;
59+ if data. sub_functions . len ( ) > 1 {
60+ _ = data. sub_functions [ 1 ] . execute ( sandbox) . pop ( ) ;
61+ }
62+ }
63+
64+ println ! ( "Comparison: left {:?}, right: {:?}" , data. values[ 0 ] , v) ;
65+ }
66+ }
67+
68+ TheCodeNodeCallResult :: Continue
69+ } ;
70+
71+ let mut node = TheCodeNode :: new (
72+ call,
73+ TheCodeNodeData :: location_values ( ctx. node_location , vec ! [ TheValue :: Int ( 0 ) ] ) ,
74+ ) ;
75+
76+ if let Some ( function) = ctx. remove_function ( ) {
77+ // let mut sandbox = TheCodeSandbox::new();
78+ // if let Some(v) = function.execute(&mut sandbox).pop() {
79+ // println!("{:?}", v);//
80+ // node.data.values[1] = v;
81+ // }
82+ node. data . sub_functions . push ( function) ;
83+ }
84+
85+ node
86+ }
4987 // Generates a pulse gate.
5088 TheCodeAtom :: Pulse => {
5189 let call: TheCodeNodeCall =
@@ -98,7 +136,7 @@ impl TheCodeAtom {
98136
99137 node
100138 }
101- TheCodeAtom :: Assignment ( _op ) => {
139+ TheCodeAtom :: Assignment ( _ ) => {
102140 let call: TheCodeNodeCall =
103141 |_stack : & mut Vec < TheValue > ,
104142 _data : & mut TheCodeNodeData ,
@@ -390,7 +428,6 @@ impl TheCodeAtom {
390428 _sandbox : & mut TheCodeSandbox | {
391429 if let Some ( b) = stack. pop ( ) {
392430 if let Some ( a) = stack. pop ( ) {
393- println ! ( "t {:?} {:?}" , a, b) ;
394431 if let Some ( result) = TheValue :: add ( & a, & b) {
395432 stack. push ( result) ;
396433 } else {
@@ -438,33 +475,6 @@ impl TheCodeAtom {
438475 } ;
439476 TheCodeNode :: new ( call, TheCodeNodeData :: location ( ctx. current_location ) )
440477 }
441- TheCodeAtom :: Switch => {
442- let call: TheCodeNodeCall =
443- |_stack : & mut Vec < TheValue > ,
444- _data : & mut TheCodeNodeData ,
445- _sandbox : & mut TheCodeSandbox | {
446- TheCodeNodeCallResult :: Continue
447- } ;
448- TheCodeNode :: new ( call, TheCodeNodeData :: location ( ctx. current_location ) )
449- }
450- TheCodeAtom :: CaseCondition => {
451- let call: TheCodeNodeCall =
452- |_stack : & mut Vec < TheValue > ,
453- _data : & mut TheCodeNodeData ,
454- _sandbox : & mut TheCodeSandbox | {
455- TheCodeNodeCallResult :: Continue
456- } ;
457- TheCodeNode :: new ( call, TheCodeNodeData :: location ( ctx. current_location ) )
458- }
459- TheCodeAtom :: CaseBody => {
460- let call: TheCodeNodeCall =
461- |_stack : & mut Vec < TheValue > ,
462- _data : & mut TheCodeNodeData ,
463- _sandbox : & mut TheCodeSandbox | {
464- TheCodeNodeCallResult :: Continue
465- } ;
466- TheCodeNode :: new ( call, TheCodeNodeData :: location ( ctx. current_location ) )
467- }
468478 }
469479 }
470480
@@ -485,9 +495,18 @@ impl TheCodeAtom {
485495 }
486496 }
487497
498+ pub fn to_color ( & self ) -> [ u8 ; 4 ] {
499+ match self {
500+ Self :: ObjectSet ( _, _) => [ 36 , 61 , 92 , 255 ] ,
501+ //[87, 112, 143, 255]
502+ _ => [ 174 , 174 , 174 , 255 ] ,
503+ }
504+ }
505+
488506 pub fn to_kind ( & self ) -> TheCodeAtomKind {
489507 match self {
490508 TheCodeAtom :: Assignment ( _op) => TheCodeAtomKind :: Equal ,
509+ TheCodeAtom :: Comparison ( _op) => TheCodeAtomKind :: Equal ,
491510 TheCodeAtom :: FuncDef ( _name) => TheCodeAtomKind :: Fn ,
492511 TheCodeAtom :: FuncArg ( _name) => TheCodeAtomKind :: Identifier ,
493512 TheCodeAtom :: FuncCall ( _name) => TheCodeAtomKind :: Identifier ,
@@ -503,15 +522,13 @@ impl TheCodeAtom {
503522 TheCodeAtom :: Multiply => TheCodeAtomKind :: Star ,
504523 TheCodeAtom :: EndOfExpression => TheCodeAtomKind :: Semicolon ,
505524 TheCodeAtom :: EndOfCode => TheCodeAtomKind :: Eof ,
506- TheCodeAtom :: Switch => TheCodeAtomKind :: If ,
507- TheCodeAtom :: CaseCondition => TheCodeAtomKind :: If ,
508- TheCodeAtom :: CaseBody => TheCodeAtomKind :: If ,
509525 }
510526 }
511527
512528 pub fn describe ( & self ) -> String {
513529 match self {
514530 TheCodeAtom :: Assignment ( op) => op. clone ( ) ,
531+ TheCodeAtom :: Comparison ( op) => op. clone ( ) ,
515532 TheCodeAtom :: FuncDef ( name) => name. clone ( ) ,
516533 TheCodeAtom :: FuncArg ( name) => name. clone ( ) ,
517534 TheCodeAtom :: FuncCall ( name) => name. clone ( ) ,
@@ -530,15 +547,13 @@ impl TheCodeAtom {
530547 TheCodeAtom :: Multiply => "*" . to_string ( ) ,
531548 TheCodeAtom :: EndOfExpression => ";" . to_string ( ) ,
532549 TheCodeAtom :: EndOfCode => "Stop" . to_string ( ) ,
533- TheCodeAtom :: Switch => "Switch" . to_string ( ) ,
534- TheCodeAtom :: CaseCondition => "Case" . to_string ( ) ,
535- TheCodeAtom :: CaseBody => ":" . to_string ( ) ,
536550 }
537551 }
538552
539553 pub fn help ( & self ) -> String {
540554 match self {
541555 TheCodeAtom :: Assignment ( name) => format ! ( "Assignment ({})." , name) ,
556+ TheCodeAtom :: Comparison ( name) => format ! ( "Comparison ({})." , name) ,
542557 TheCodeAtom :: FuncDef ( name) => format ! ( "Function definition ({})." , name) ,
543558 TheCodeAtom :: FuncArg ( name) => format ! ( "Function argument ({})." , name) ,
544559 TheCodeAtom :: FuncCall ( name) => format ! (
@@ -581,9 +596,6 @@ impl TheCodeAtom {
581596 TheCodeAtom :: Multiply => "Operator ('*')" . to_string ( ) ,
582597 TheCodeAtom :: EndOfExpression => ";" . to_string ( ) ,
583598 TheCodeAtom :: EndOfCode => "Stop" . to_string ( ) ,
584- TheCodeAtom :: Switch => "Switch statement." . to_string ( ) ,
585- TheCodeAtom :: CaseCondition => "Switch 'Case' statement." . to_string ( ) ,
586- TheCodeAtom :: CaseBody => "Switch 'Case' body." . to_string ( ) ,
587599 }
588600 }
589601
0 commit comments