Skip to content

Commit

Permalink
feat: enhance Menu structure to include field attribute and update re…
Browse files Browse the repository at this point in the history
…lated logic
  • Loading branch information
aspizu committed Jan 21, 2025
1 parent e9d97b9 commit e1f142f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 11 deletions.
2 changes: 1 addition & 1 deletion editors/code/syntaxes/goboscript.tmGrammar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ patterns:
- name: keyword
match: "\\b(error|warn|breakpoint|local|not|and|or|in|length|round|abs|floor|ceil|sqrt|sin|cos|tan|asin|acos|atan|ln|log|antiln|antilog)\\b"
- name: support.function.builtin
match: "\\b(move|turn_left|turn_right|goto_random_position|goto_mouse_pointer|goto|glide|glide_to_random_position|glide_to_mouse_pointer|point_in_direction|point_towards_mouse_pointer|point_towards_random_direction|point_towards|change_x|set_x|change_y|set_y|if_on_edge_bounce|set_rotation_style_left_right|set_rotation_style_do_not_rotate|set_rotation_style_all_around|say|think|switch_costume|next_costume|switch_backdrop|next_backdrop|set_size|change_size|change_color_effect|change_fisheye_effect|change_whirl_effect|change_pixelate_effect|change_mosaic_effect|change_brightness_effect|change_ghost_effect|set_color_effect|set_fisheye_effect|set_whirl_effect|set_pixelate_effect|set_mosaic_effect|set_brightness_effect|set_ghost_effect|clear_graphic_effects|show|hide|goto_front|goto_back|go_forward|go_backward|play_sound_until_done|start_sound|stop_all_sounds|change_pitch_effect|change_pan_effect|set_pitch_effect|set_pan_effect|change_volume|set_volume|clear_sound_effects|broadcast|broadcast_and_wait|wait|wait_until|stop_all|stop_this_script|stop_other_scripts|delete_this_clone|clone|ask|set_drag_mode_draggable|set_drag_mode_not_draggable|reset_timer|erase_all|stamp|pen_down|pen_up|set_pen_color|change_pen_size|set_pen_size|rest|set_tempo|change_tempo)\\b"
match: "\\b(move|turn_left|turn_right|goto_random_position|goto_mouse_pointer|goto|glide|glide_to_random_position|glide_to_mouse_pointer|point_in_direction|point_towards_mouse_pointer|point_towards_random_direction|point_towards|change_x|set_x|change_y|set_y|if_on_edge_bounce|set_rotation_style_left_right|set_rotation_style_do_not_rotate|set_rotation_style_all_around|say|think|switch_costume|next_costume|switch_backdrop|next_backdrop|set_size|change_size|change_color_effect|change_fisheye_effect|change_whirl_effect|change_pixelate_effect|change_mosaic_effect|change_brightness_effect|change_ghost_effect|set_color_effect|set_fisheye_effect|set_whirl_effect|set_pixelate_effect|set_mosaic_effect|set_brightness_effect|set_ghost_effect|clear_graphic_effects|show|hide|goto_front|goto_back|go_forward|go_backward|play_sound_until_done|start_sound|stop_all_sounds|change_pitch_effect|change_pan_effect|set_pitch_effect|set_pan_effect|change_volume|set_volume|clear_sound_effects|broadcast|broadcast_and_wait|wait|wait_until|stop_all|stop_this_script|stop_other_scripts|delete_this_clone|clone|ask|set_drag_mode_draggable|set_drag_mode_not_draggable|reset_timer|erase_all|stamp|pen_down|pen_up|set_pen_color|change_pen_size|set_pen_size|set_pen_hue|set_pen_saturation|set_pen_brightness|set_pen_transparency|rest|set_tempo|change_tempo)\\b"
- name: entity.name.type
match: "\\b(x_position|y_position|direction|size|costume_number|costume_name|backdrop_number|backdrop_name|volume|touching_mouse_pointer|touching_edge|touching|key_pressed|mouse_down|mouse_x|mouse_y|loudness|timer|current_year|current_month|current_date|current_day_of_week|current_hour|current_minute|current_second|days_since_2000|username|touching_color|color_is_touching_color|answer|random)\\b"
- name: punctuation
Expand Down
21 changes: 14 additions & 7 deletions gdsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BinOp:
@dataclass
class Menu:
input: str
field: str
opcode: str
default: str

Expand Down Expand Up @@ -130,8 +131,13 @@ def parse():
input_opcode = old_menu
old_menu = input_opcode
input, opcode = input_opcode.split(":")
if "@" in input:
input, field = input.split("@")
else:
field = input
menu = Menu(
input=input,
field=field,
opcode=opcode,
default=default,
)
Expand Down Expand Up @@ -184,6 +190,7 @@ def parse():
pub input: &'static str,
pub opcode: &'static str,
pub default: &'static str,
pub field: &'static str,
}
""")
f.write("#[derive(Debug, Copy, Clone)]\npub enum UnOp {")
Expand Down Expand Up @@ -218,7 +225,7 @@ def parse():
f.write(f"Self::{variant} => None,")
else:
f.write(
f"Self::{variant} => Some({json.dumps(json.dumps({k:[v,None] for k,v in op.fields.items()}))}),"
f"Self::{variant} => Some({json.dumps(json.dumps({k: [v, None] for k, v in op.fields.items()}))}),"
)
f.write("_ => unreachable!()")
f.write("}")
Expand Down Expand Up @@ -277,11 +284,11 @@ def write_blocks(typename: str, blocks: dict[str, Block | list[Block]]):
avariant = f"{variant}{len(block.args)}"
if block.menu:
f.write(
f"Self::{avariant} => Some(Menu {{ opcode: {json.dumps(block.menu.opcode)}, input: {json.dumps(block.menu.input)}, default: {json.dumps(block.menu.default)} }}),"
f"Self::{avariant} => Some(Menu {{ opcode: {json.dumps(block.menu.opcode)}, input: {json.dumps(block.menu.input)}, field: {json.dumps(block.menu.field)}, default: {json.dumps(block.menu.default)} }}),"
)
elif block.menu:
f.write(
f"Self::{variant} => Some(Menu {{ opcode: {json.dumps(block.menu.opcode)}, input: {json.dumps(block.menu.input)}, default: {json.dumps(block.menu.default)} }}),"
f"Self::{variant} => Some(Menu {{ opcode: {json.dumps(block.menu.opcode)}, input: {json.dumps(block.menu.input)}, field: {json.dumps(block.menu.field)}, default: {json.dumps(block.menu.default)} }}),"
)
f.write("_ => None }")
f.write("}\n\n")
Expand Down Expand Up @@ -371,14 +378,14 @@ def write_blocks(typename: str, blocks: dict[str, Block | list[Block]]):
f.write(f"Self::{variant}{len(block.args)} => None,")
continue
f.write(
f"Self::{variant}{len(block.args)} => Some({json.dumps(json.dumps({k:[v,None] for k,v in block.fields.items()}))}),"
f"Self::{variant}{len(block.args)} => Some({json.dumps(json.dumps({k: [v, None] for k, v in block.fields.items()}))}),"
)
else:
if len(block.fields) == 0:
f.write(f"Self::{variant} => None,")
continue
f.write(
f"Self::{variant} => Some({json.dumps(json.dumps({k:[v,None] for k,v in block.fields.items()}))}),"
f"Self::{variant} => Some({json.dumps(json.dumps({k: [v, None] for k, v in block.fields.items()}))}),"
)
f.write("}")
f.write("}\n\n")
Expand All @@ -390,12 +397,12 @@ def write_blocks(typename: str, blocks: dict[str, Block | list[Block]]):

print(
json.dumps(
f'\\b({"|".join(block.name if isinstance(block, Block) else block[0].name for block in blocks.values() )})\\b'
f"\\b({'|'.join(block.name if isinstance(block, Block) else block[0].name for block in blocks.values())})\\b"
)
)
print()
print(
json.dumps(
f'\\b({"|".join(block.name if isinstance(block, Block) else block[0].name for block in reporters.values() )})\\b'
f"\\b({'|'.join(block.name if isinstance(block, Block) else block[0].name for block in reporters.values())})\\b"
)
)
4 changes: 4 additions & 0 deletions gdsl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ pen_up penUp |
set_pen_color setPenColorToColor COLOR | |
change_pen_size changePenSizeBy SIZE | |
set_pen_size setPenSizeTo ... | |
set_pen_hue setPenColorParamTo VALUE | | COLOR_PARAM@colorParam:pen_menu_colorParam=color
set_pen_saturation setPenColorParamTo VALUE | | COLOR_PARAM@colorParam:pen_menu_colorParam=saturation
set_pen_brightness setPenColorParamTo VALUE | | COLOR_PARAM@colorParam:pen_menu_colorParam=brightness
set_pen_transparency setPenColorParamTo VALUE | | COLOR_PARAM@colorParam:pen_menu_colorParam=transparency
[music]==========================================================|==========================|
rest restForBeats BEATS | |
set_tempo setTempo TEMPO | |
Expand Down
76 changes: 75 additions & 1 deletion src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub struct Menu {
pub input: &'static str,
pub opcode: &'static str,
pub default: &'static str,
pub field: &'static str,
}
#[derive(Debug, Copy, Clone)]
pub enum UnOp {
Expand Down Expand Up @@ -262,6 +263,10 @@ pub enum Block {
SetPenColor,
ChangePenSize,
SetPenSize,
SetPenHue,
SetPenSaturation,
SetPenBrightness,
SetPenTransparency,
Rest,
SetTempo,
ChangeTempo,
Expand All @@ -273,78 +278,117 @@ impl Block {
Self::GotoRandomPosition => Some(Menu {
opcode: "motion_goto_menu",
input: "TO",
field: "TO",
default: "_random_",
}),
Self::GotoMousePointer => Some(Menu {
opcode: "motion_goto_menu",
input: "TO",
field: "TO",
default: "_mouse_",
}),
Self::Goto1 => Some(Menu {
opcode: "motion_goto_menu",
input: "TO",
field: "TO",
default: "_random_",
}),
Self::Glide2 => Some(Menu {
opcode: "motion_glideto_menu",
input: "TO",
field: "TO",
default: "_random_",
}),
Self::GlideToRandomPosition => Some(Menu {
opcode: "motion_glideto_menu",
input: "TO",
field: "TO",
default: "_random_",
}),
Self::GlideToMousePointer => Some(Menu {
opcode: "motion_glideto_menu",
input: "TO",
field: "TO",
default: "_mouse_",
}),
Self::PointTowardsMousePointer => Some(Menu {
opcode: "motion_pointtowards_menu",
input: "TOWARDS",
field: "TOWARDS",
default: "_mouse_",
}),
Self::PointTowardsRandomDirection => Some(Menu {
opcode: "motion_pointtowards_menu",
input: "TOWARDS",
field: "TOWARDS",
default: "_random_",
}),
Self::PointTowards => Some(Menu {
opcode: "motion_pointtowards_menu",
input: "TOWARDS",
field: "TOWARDS",
default: "_random_",
}),
Self::SwitchCostume => Some(Menu {
opcode: "looks_costume",
input: "COSTUME",
field: "COSTUME",
default: "make gh issue if this bothers u",
}),
Self::SwitchBackdrop => Some(Menu {
opcode: "looks_backdrops",
input: "BACKDROP",
field: "BACKDROP",
default: "make gh issue if this bothers u",
}),
Self::PlaySoundUntilDone => Some(Menu {
opcode: "sound_sounds_menu",
input: "SOUND_MENU",
field: "SOUND_MENU",
default: "make gh issue if this bothers u",
}),
Self::StartSound => Some(Menu {
opcode: "sound_sounds_menu",
input: "SOUND_MENU",
field: "SOUND_MENU",
default: "make gh issue if this bothers u",
}),
Self::Clone0 => Some(Menu {
opcode: "control_create_clone_of_menu",
input: "CLONE_OPTION",
field: "CLONE_OPTION",
default: "_myself_",
}),
Self::Clone1 => Some(Menu {
opcode: "control_create_clone_of_menu",
input: "CLONE_OPTION",
field: "CLONE_OPTION",
default: "_myself_",
}),
Self::SetPenHue => Some(Menu {
opcode: "pen_menu_colorParam",
input: "COLOR_PARAM",
field: "colorParam",
default: "color",
}),
Self::SetPenSaturation => Some(Menu {
opcode: "pen_menu_colorParam",
input: "COLOR_PARAM",
field: "colorParam",
default: "saturation",
}),
Self::SetPenBrightness => Some(Menu {
opcode: "pen_menu_colorParam",
input: "COLOR_PARAM",
field: "colorParam",
default: "brightness",
}),
Self::SetPenTransparency => Some(Menu {
opcode: "pen_menu_colorParam",
input: "COLOR_PARAM",
field: "colorParam",
default: "transparency",
}),
_ => None,
}
}
Expand Down Expand Up @@ -452,6 +496,10 @@ impl Block {
("set_pen_color", _) => Some(Self::SetPenColor),
("change_pen_size", _) => Some(Self::ChangePenSize),
("set_pen_size", _) => Some(Self::SetPenSize),
("set_pen_hue", _) => Some(Self::SetPenHue),
("set_pen_saturation", _) => Some(Self::SetPenSaturation),
("set_pen_brightness", _) => Some(Self::SetPenBrightness),
("set_pen_transparency", _) => Some(Self::SetPenTransparency),
("rest", _) => Some(Self::Rest),
("set_tempo", _) => Some(Self::SetTempo),
("change_tempo", _) => Some(Self::ChangeTempo),
Expand Down Expand Up @@ -546,6 +594,10 @@ impl Block {
Self::SetPenColor => "set_pen_color",
Self::ChangePenSize => "change_pen_size",
Self::SetPenSize => "set_pen_size",
Self::SetPenHue => "set_pen_hue",
Self::SetPenSaturation => "set_pen_saturation",
Self::SetPenBrightness => "set_pen_brightness",
Self::SetPenTransparency => "set_pen_transparency",
Self::Rest => "rest",
Self::SetTempo => "set_tempo",
Self::ChangeTempo => "change_tempo",
Expand Down Expand Up @@ -634,6 +686,10 @@ impl Block {
"set_pen_color",
"change_pen_size",
"set_pen_size",
"set_pen_hue",
"set_pen_saturation",
"set_pen_brightness",
"set_pen_transparency",
"rest",
"set_tempo",
"change_tempo",
Expand Down Expand Up @@ -727,6 +783,10 @@ impl Block {
Self::SetPenColor => "pen_setPenColorToColor",
Self::ChangePenSize => "pen_changePenSizeBy",
Self::SetPenSize => "pen_setPenSizeTo",
Self::SetPenHue => "pen_setPenColorParamTo",
Self::SetPenSaturation => "pen_setPenColorParamTo",
Self::SetPenBrightness => "pen_setPenColorParamTo",
Self::SetPenTransparency => "pen_setPenColorParamTo",
Self::Rest => "music_restForBeats",
Self::SetTempo => "music_setTempo",
Self::ChangeTempo => "music_changeTempo",
Expand Down Expand Up @@ -820,6 +880,10 @@ impl Block {
Self::SetPenColor => &["COLOR"],
Self::ChangePenSize => &["SIZE"],
Self::SetPenSize => &["SIZE"],
Self::SetPenHue => &["VALUE"],
Self::SetPenSaturation => &["VALUE"],
Self::SetPenBrightness => &["VALUE"],
Self::SetPenTransparency => &["VALUE"],
Self::Rest => &["BEATS"],
Self::SetTempo => &["TEMPO"],
Self::ChangeTempo => &["TEMPO"],
Expand Down Expand Up @@ -915,6 +979,10 @@ impl Block {
Self::SetPenColor => None,
Self::ChangePenSize => None,
Self::SetPenSize => None,
Self::SetPenHue => None,
Self::SetPenSaturation => None,
Self::SetPenBrightness => None,
Self::SetPenTransparency => None,
Self::Rest => None,
Self::SetTempo => None,
Self::ChangeTempo => None,
Expand Down Expand Up @@ -962,29 +1030,35 @@ impl Repr {
Self::TouchingMousePointer => Some(Menu {
opcode: "sensing_touchingobjectmenu",
input: "TOUCHINGOBJECTMENU",
field: "TOUCHINGOBJECTMENU",
default: "_mouse_",
}),
Self::TouchingEdge => Some(Menu {
opcode: "sensing_touchingobjectmenu",
input: "TOUCHINGOBJECTMENU",
field: "TOUCHINGOBJECTMENU",
default: "_edge_",
}),
Self::Touching => Some(Menu {
opcode: "sensing_touchingobjectmenu",
input: "TOUCHINGOBJECTMENU",
field: "TOUCHINGOBJECTMENU",
default: "_mouse_",
}),
Self::KeyPressed => Some(Menu {
opcode: "sensing_keyoptions",
input: "KEY_OPTION",
field: "KEY_OPTION",
default: "any",
}),
_ => None,
}
}

pub fn overloads(name: &str) -> &'static [Self] {
&[]
match name {
_ => &[],
}
}

pub fn from_shape(name: &str, args: usize) -> Option<Self> {
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ where T: Write + Seek
.shadow(true),
)?;
if let Some(menu_value) = menu_value {
self.single_field(menu.input, &menu_value.to_string())?;
self.single_field(menu.field, &menu_value.to_string())?;
} else {
self.single_field(menu.input, menu.default)?;
self.single_field(menu.field, menu.default)?;
}
self.end_obj()?; // node
}
Expand Down

0 comments on commit e1f142f

Please sign in to comment.