added variables
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 19 Feb 2021 00:31:06 +0000 (18:31 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 19 Feb 2021 00:31:06 +0000 (18:31 -0600)
dist/prez.wasm
onyx.prez
src/show_parser.onyx
src/slides.onyx

index 75cf0cdaf2790f811de93bc2f01dbcd97f2a3ef9..598ac5a4ea5db3dd4ff0b49896bef2c830ecb6a5 100644 (file)
Binary files a/dist/prez.wasm and b/dist/prez.wasm differ
index b81dfffa8c17de33808219770ae44b0e766d5405..5b38c68e2df51f2310058c3a7c568b3e9bb0b54b 100644 (file)
--- a/onyx.prez
+++ b/onyx.prez
@@ -1,11 +1,18 @@
 
-[define_text_style normal color 255 255 255 font_size 40 font_name "Arial" left padding 10]
-[define_text_style title color 255 255 255 font_attr bold font_size 72 font_name "Arial" centered]
-[define_text_style subtitle color 255 255 255 font_attr italic font_size 72 font_name "Arial" centered]
-[define_text_style header color 255 255 255 font_attr bold font_size 72 font_name "Arial" left padding 10]
+[var tbright 236]
+[var bbright 20]
+
+[define_text_style normal color   $tbright $tbright $tbright font_size 40 font_name "Arial" left padding 10]
+[define_text_style title color    $tbright $tbright $tbright font_attr bold font_size 72 font_name "Arial" centered]
+[define_text_style subtitle color $tbright $tbright $tbright font_attr italic font_size 72 font_name "Arial" centered]
+[define_text_style header color   $tbright $tbright $tbright font_attr bold font_size 72 font_name "Arial" left padding 10]
+
+[define_text_style hyperlink color 100 100 255 font_attr italic font_size 40 font_name "Arial" left padding 10]
+
+[load_image wasm_logo "https://webassembly.org/css/webassembly.svg"]
 
 # ---------------------------------------------
-[slide] [background 30 30 30]
+[slide] [background $bbright $bbright $bbright]
 
 [rect color 0 0 0 x 0 w 100 y 40 h 20] 
 
 # Introduce what Onyx is, the design goals, the improvements over C, etc.
 
 [slide]
-[rect color 0 0 0 x 0 w 100 y 5 h 10]
+[rect color 40 40 40 x 8 w 84  y 0 h 100]
+[rect color 0  0  0  x 0 w 100 y 5 h 10]
 [text_style header]
 [y 12] What is Onyx?
 
 [text_style normal]
-[y 20] ▪ New programming language for WebAssembly
-[y 25] ▪ No dependence on other langauges
-[y 30] ▪ Fast compilation by design
-[y 35] ▪ Easy to write by design
+[y 22] New programming language for WebAssembly developed entirely by me.
 
+[y 34] Some important design features:
+[text_style inherit padding 15]
+▪ No dependence on other langauges
+▪ Fast compilation
+▪ Easy to write
+▪ Easy to read
+
+
+# ---------------------------------------------
+# BRIEFLY introduce WASM and its capabilities
 
 [slide]
-[rect color 0 0 0 x 0 w 100 y 5 h 10]
+[rect color 40 40 40 x 8 w 84  y 0 h 100]
+[rect color 0  0  0  x 0 w 100 y 5 h 10]
 [text_style header]
-[y 12] Design of Onyx
+[y 12] Brief aside: WebAssembly
 
 [text_style normal]
-[y 20] 
+[y 20] WebAssembly (WASM for short) is a new execution platform for the web.
+
+[y 30] It defines:
+[text_style inherit padding 15]
+▪ A Virtual Instruction Set Architecture
+▪ Linear Memory Model
+▪ Arbitrary imports
+▪ Safe indirect function calls
+
+[text_style normal centered]
+[y 75] For more information, you can check out,
+[text_style hyperlink centered]
+https://webassembly.org/
+
+[image wasm_logo x 65 y 34 width 20]
+
+
+# ---------------------------------------------
+# High-level language features and design that make it super cozy to program in.
+
+[slide]
+[rect color 40 40 40 x 8 w 84  y 0 h 100]
+[rect color 0  0  0  x 0 w 100 y 5 h 10]
+[text_style header]
+[y 12] Design of Onyx
+
+[text_style normal font_attr italic font_size 44]
+[y 20] Declare anywhere.
+Polymorphic procedures and structures.
+Package system.
index 38fb95cf32344c8e35e057885d20c8d3d20d9dc0..a3c5d27feb3d21b6a3dbade9dc1d2bcfa1ec7dad 100644 (file)
@@ -1,5 +1,10 @@
 use package core
 
+ParseContext :: struct {
+    reader: ^io.Reader;
+    variables: ^map.Map(str, u32);
+}
+
 parse_slideshow :: (source: str, slideshow: ^Slideshow) {
     use io
 
@@ -21,6 +26,11 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
     text_styles := map.make(str, Slide_Item_Text, default_text_style);
     defer map.free(^text_styles);
 
+    variables := map.make(str, u32);
+    defer map.free(^variables);
+
+    parse_context := ParseContext.{ ^show_reader, ^variables };
+
     current_slide: ^Slide;
     current_item_idx := 0;
     current_text_style: Slide_Item_Text;
@@ -48,7 +58,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                     slide_init(current_slide, background_color=prev_slide.background, item_count = 16);
                 }
                 elseif command_name == "background" {
-                    r, g, b := parse_color(^show_reader);
+                    r, g, b := parse_color(^parse_context);
                     current_slide.background = Color.{ r, g, b };
                 }
                 elseif command_name == "y" {
@@ -62,27 +72,33 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                     }
 
                     text_style := map.get(^text_styles, text_style_name);
+                    
+                    old_y := current_text_style.y_pos;
                     current_text_style = text_style;
+                    current_text_style.y_pos = old_y;
+
+                    parse_text_style(^parse_context, ^current_text_style, parse_alloc);
 
-                    parse_text_style(^show_reader, ^current_text_style, parse_alloc);
+                    // So 'inherit' can be used to copy all existing text styling.
+                    map.put(^text_styles, "inherit", current_text_style);
                 }
                 elseif command_name == "define_text_style" {
                     text_style_name := read_word(^show_reader, numeric_allowed=true);
 
                     current_text_style = default_text_style;
-                    parse_text_style(^show_reader, ^current_text_style, parse_alloc);
+                    parse_text_style(^parse_context, ^current_text_style, parse_alloc);
                     map.put(^text_styles, text_style_name, current_text_style);
                 }
                 elseif command_name == "load_image" {
                     image_name := read_word(^show_reader, numeric_allowed=true);
-                    image_path := parse_string(^show_reader, allocator=parse_alloc);
+                    image_path := parse_string(^parse_context, allocator=parse_alloc);
 
                     slideshow_load_image(slideshow, image_name, image_path);
                 }
                 elseif command_name == "image" {
                     new_slide_image := slideshow_make_item(slideshow);
                     new_slide_image.kind = Slide_Item.Kind.Image;
-                    parse_image_style(^show_reader, ^new_slide_image.image, parse_alloc);
+                    parse_image_style(^parse_context, ^new_slide_image.image, parse_alloc);
 
                     current_slide.items[current_item_idx] = new_slide_image;
                     current_item_idx += 1;
@@ -90,11 +106,17 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
                 elseif command_name == "rect" {
                     new_slide_rect := slideshow_make_item(slideshow);
                     new_slide_rect.kind = Slide_Item.Kind.Rect;
-                    parse_rect_style(^show_reader, ^new_slide_rect.rect, parse_alloc);
+                    parse_rect_style(^parse_context, ^new_slide_rect.rect, parse_alloc);
 
                     current_slide.items[current_item_idx] = new_slide_rect;
                     current_item_idx += 1;
                 }
+                elseif command_name == "var" {
+                    var_name := read_word(^show_reader, numeric_allowed=true);
+                    var_value := read_u32(^show_reader);
+
+                    map.put(^variables, var_name, var_value);
+                }
                 else {
                     printf("******** Unknown command: '%s'.\n", command_name);
                 }
@@ -116,6 +138,11 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
 
                 current_slide.items[current_item_idx] = new_slide_text;
                 current_item_idx += 1;
+
+                // @Robustness
+                // This feels like the right amount, but obviously this should change
+                // depending on font size, window height, etc...
+                current_text_style.y_pos += .05;
             }
         }
     }
@@ -124,7 +151,7 @@ parse_slideshow :: (source: str, slideshow: ^Slideshow) {
 }
 
 #private_file
-parse_text_style :: (reader: ^io.Reader, text_style: ^Slide_Item_Text, parse_alloc := context.allocator) {
+parse_text_style :: (use pc: ^ParseContext, text_style: ^Slide_Item_Text, parse_alloc := context.allocator) {
     use io
 
     while !stream_end_of_file(reader.stream) && peek_byte(reader) != #char "]" {
@@ -133,14 +160,14 @@ parse_text_style :: (reader: ^io.Reader, text_style: ^Slide_Item_Text, parse_all
         style_name := read_word(reader, allocator=parse_alloc);
         
         if style_name == "color" {
-            r, g, b := parse_color(reader);
+            r, g, b := parse_color(pc);
             text_style.color = Color.{ r, g, b };
         }
         elseif style_name == "font_size" {
-            text_style.font_size = read_u32(reader);
+            text_style.font_size = parse_numeric_value(pc);
         }
         elseif style_name == "font_name" {
-            text_style.font_name = parse_string(reader);
+            text_style.font_name = parse_string(pc);
         }
         elseif style_name == "font_attr" {
             font_attr := read_word(reader, allocator=parse_alloc);
@@ -149,7 +176,7 @@ parse_text_style :: (reader: ^io.Reader, text_style: ^Slide_Item_Text, parse_all
             elseif font_attr == "italic" do text_style.font_attr |= Slide_Item_Text.FontAttributes.Italic;
         }
         elseif style_name == "padding" {
-            padding := read_u32(reader);
+            padding := parse_numeric_value(pc);
             text_style.padding = cast(f32) padding / 100;
         }
         elseif style_name == "left"     do text_style.justify = Slide_Item_Text.Justify.Left;
@@ -164,7 +191,7 @@ parse_text_style :: (reader: ^io.Reader, text_style: ^Slide_Item_Text, parse_all
 }
 
 #private_file
-parse_image_style :: (reader: ^io.Reader, image_style: ^Slide_Item_Image, parse_alloc := context.allocator) {
+parse_image_style :: (use pc: ^ParseContext, image_style: ^Slide_Item_Image, parse_alloc := context.allocator) {
     use io
 
     image_name := read_word(reader, numeric_allowed=true);
@@ -176,15 +203,15 @@ parse_image_style :: (reader: ^io.Reader, image_style: ^Slide_Item_Image, parse_
         style_name := read_word(reader, allocator=parse_alloc);
         
         if style_name == "x" {
-            x := read_u32(reader);
+            x := parse_numeric_value(pc);
             image_style.x = cast(f32) x / 100;
         }
         elseif style_name == "y" {
-            y := read_u32(reader);
+            y := parse_numeric_value(pc);
             image_style.y = cast(f32) y / 100;
         }
         elseif style_name == "width" {
-            width := read_u32(reader);
+            width := parse_numeric_value(pc);
             image_style.width = cast(f32) width / 100;
         }
         else {
@@ -196,7 +223,7 @@ parse_image_style :: (reader: ^io.Reader, image_style: ^Slide_Item_Image, parse_
 }
 
 #private_file
-parse_rect_style :: (reader: ^io.Reader, rect_style: ^Slide_Item_Rect, parse_alloc := context.allocator) {
+parse_rect_style :: (use pc: ^ParseContext, rect_style: ^Slide_Item_Rect, parse_alloc := context.allocator) {
     use io
 
     while !stream_end_of_file(reader.stream) && peek_byte(reader) != #char "]" {
@@ -205,23 +232,23 @@ parse_rect_style :: (reader: ^io.Reader, rect_style: ^Slide_Item_Rect, parse_all
         style_name := read_word(reader, allocator=parse_alloc);
         
         if style_name == "x" {
-            x := read_u32(reader);
+            x := parse_numeric_value(pc);
             rect_style.x = cast(f32) x / 100;
         }
         elseif style_name == "y" {
-            y := read_u32(reader);
+            y := parse_numeric_value(pc);
             rect_style.y = cast(f32) y / 100;
         }
         elseif style_name == "w" {
-            w := read_u32(reader);
+            w := parse_numeric_value(pc);
             rect_style.w = cast(f32) w / 100;
         }
         elseif style_name == "h" {
-            h := read_u32(reader);
+            h := parse_numeric_value(pc);
             rect_style.h = cast(f32) h / 100;
         }
         elseif style_name == "color" {
-            r, g, b := parse_color(reader);
+            r, g, b := parse_color(pc);
             rect_style.color = Color.{ r, g, b };
         }
         else {
@@ -233,10 +260,34 @@ parse_rect_style :: (reader: ^io.Reader, rect_style: ^Slide_Item_Rect, parse_all
 }
 
 #private_file
-parse_color :: (reader: ^io.Reader) -> (f32, f32, f32) {
-    r := io.read_u32(reader);
-    g := io.read_u32(reader);
-    b := io.read_u32(reader);
+parse_numeric_value :: (use pc: ^ParseContext) -> u32 {
+    use io
+
+    skip_whitespace(reader);
+    _, next_byte := stream_peek_byte(reader.stream);
+    if next_byte == #char "$" {
+        io.read_byte(reader);
+        var_name := read_word(reader, numeric_allowed=true);
+
+        value := 0;
+
+        if map.has(variables, var_name) {
+            value = map.get(variables, var_name);
+        } else {
+            printf("Variable '%s' was never defined!\n", var_name);
+        }
+
+        return value;
+    }
+
+    return read_u32(reader);
+}
+
+#private_file
+parse_color :: (use pc: ^ParseContext) -> (f32, f32, f32) {
+    r := parse_numeric_value(pc);
+    g := parse_numeric_value(pc);
+    b := parse_numeric_value(pc);
 
     fr := cast(f32) r / 255;
     fg := cast(f32) g / 255;
@@ -246,7 +297,7 @@ parse_color :: (reader: ^io.Reader) -> (f32, f32, f32) {
 }
 
 #private_file
-parse_string :: (reader: ^io.Reader, allocator := context.allocator) -> str {
+parse_string :: (use pc: ^ParseContext, allocator := context.allocator) -> str {
     use io
 
     // @Cleanup
index 44f1cf5a3156895f4e96e4e8822fbbd15c0dc22c..0af702f2bcd854d2a194d09c6a31261c5ee0d799 100644 (file)
@@ -92,12 +92,18 @@ Slide_Item_Rect :: struct {
 slideshow_make :: (allocator := context.allocator) -> Slideshow {
     slideshow: Slideshow;
     slideshow_init(^slideshow, allocator);
+
+    // @Reconsider: By not reseting the current_slide to 0 in the init function,
+    // the current slide can persist over resets. This makes it easier to be
+    // editing a slideshow because you will remain on the same slide, provided
+    // no other slides were added before the slide you're working on.
+    slideshow.current_slide = 0;
+
     return slideshow;
 }
 
 slideshow_init :: (use s: ^Slideshow, allocator := context.allocator) {
     title = "Untitled Slideshow";
-    current_slide = 0;
 
     arena = alloc.arena.make(allocator, arena_size = 16 * 1024);
     array.init(^slides, 4);