src/parser.cc

changeset 82
841562f5a32f
parent 77
ad17801b1a36
child 85
264a61e9eba0
equal deleted inserted replaced
81:071715c17296 82:841562f5a32f
181 parse_block_end(); 181 parse_block_end();
182 break; 182 break;
183 183
184 case tk_const: 184 case tk_const:
185 parse_const(); 185 parse_const();
186 break;
187
188 case tk_eventdef:
189 parse_eventdef();
190 break;
191
192 case tk_funcdef:
193 parse_funcdef();
186 break; 194 break;
187 195
188 default: 196 default:
189 { 197 {
190 // Check for labels 198 // Check for labels
878 // Not found in unmarked lists, define it now 886 // Not found in unmarked lists, define it now
879 if (mark == -1) 887 if (mark == -1)
880 m_writer->add_mark (label_name); 888 m_writer->add_mark (label_name);
881 889
882 m_lx->must_get_next (tk_colon); 890 m_lx->must_get_next (tk_colon);
891 }
892
893 // =============================================================================
894 //
895 void botscript_parser::parse_eventdef()
896 {
897 event_info* e = new event_info;
898
899 m_lx->must_get_next (tk_number);
900 e->number = token_string().to_long();
901 m_lx->must_get_next (tk_colon);
902 m_lx->must_get_next (tk_symbol);
903 e->name = m_lx->get_token()->text;
904 m_lx->must_get_next (tk_paren_start);
905 m_lx->must_get_next (tk_paren_end);
906 m_lx->must_get_next (tk_semicolon);
907 add_event (e);
908 }
909
910 // =============================================================================
911 //
912 void botscript_parser::parse_funcdef()
913 {
914 command_info* comm = new command_info;
915
916 // Number
917 m_lx->must_get_next (tk_number);
918 comm->number = m_lx->get_token()->text.to_long();
919
920 m_lx->must_get_next (tk_colon);
921
922 // Name
923 m_lx->must_get_next (tk_symbol);
924 comm->name = m_lx->get_token()->text;
925
926 if (IsKeyword (comm->name))
927 error ("function name `%1` conflicts with keyword", comm->name);
928
929 m_lx->must_get_next (tk_colon);
930
931 // Return value
932 m_lx->must_get_any_of ({tk_int, tk_void, tk_bool, tk_str});
933 comm->returnvalue = GetTypeByName (m_lx->get_token()->text); // TODO
934 assert (comm->returnvalue != -1);
935
936 m_lx->must_get_next (tk_colon);
937
938 // Num args
939 m_lx->must_get_next (tk_number);
940 comm->numargs = m_lx->get_token()->text.to_long();
941
942 m_lx->must_get_next (tk_colon);
943
944 // Max args
945 m_lx->must_get_next (tk_number);
946 comm->maxargs = m_lx->get_token()->text.to_long();
947
948 // Argument types
949 int curarg = 0;
950
951 while (curarg < comm->maxargs)
952 {
953 command_argument arg;
954 m_lx->must_get_next (tk_colon);
955 m_lx->must_get_any_of ({tk_int, tk_bool, tk_str});
956 type_e type = GetTypeByName (m_lx->get_token()->text);
957 assert (type != -1 && type != TYPE_VOID);
958 arg.type = type;
959
960 m_lx->must_get_next (tk_paren_start);
961 m_lx->must_get_next (tk_symbol);
962 arg.name = m_lx->get_token()->text;
963
964 // If this is an optional parameter, we need the default value.
965 if (curarg >= comm->numargs)
966 {
967 m_lx->must_get_next (tk_assign);
968
969 switch (type)
970 {
971 case TYPE_INT:
972 case TYPE_BOOL:
973 m_lx->must_get_next (tk_number);
974 break;
975
976 case TYPE_STRING:
977 m_lx->must_get_next (tk_string);
978 break;
979
980 case TYPE_UNKNOWN:
981 case TYPE_VOID:
982 break;
983 }
984
985 arg.defvalue = m_lx->get_token()->text.to_long();
986 }
987
988 m_lx->must_get_next (tk_paren_end);
989 comm->args << arg;
990 curarg++;
991 }
992
993 m_lx->must_get_next (tk_semicolon);
994 add_command_definition (comm);
883 } 995 }
884 996
885 // ============================================================================ 997 // ============================================================================
886 // Parses a command call 998 // Parses a command call
887 data_buffer* botscript_parser::ParseCommand (command_info* comm) 999 data_buffer* botscript_parser::ParseCommand (command_info* comm)

mercurial