From: Michael D. Lowis Date: Thu, 6 Feb 2020 02:49:21 +0000 (-0500) Subject: added record and array literals X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=cf58c77e72bf65563358c61828c8b9601263476a;p=proto%2Fsclpl-rb.git added record and array literals --- diff --git a/compile.rb b/compile.rb index 7a5ef8e..0cd437b 100755 --- a/compile.rb +++ b/compile.rb @@ -157,7 +157,7 @@ class Parser RULES = { "." => { prefix: nil, infix: :access, level: :call }, - "[" => { prefix: nil, infix: :subscript, level: :call }, + "[" => { prefix: :constant, infix: :subscript, level: :call }, "(" => { prefix: :grouping, infix: :func_call, level: :call }, "+" => { prefix: :unary, infix: :binary, level: :term }, "-" => { prefix: :unary, infix: :binary, level: :term }, @@ -165,6 +165,7 @@ class Parser "/" => { prefix: nil, infix: :binary, level: :factor }, "=" => { prefix: nil, infix: :assign, level: :assign }, :ident => { prefix: :variable, infix: nil, level: :none }, + "{" => { prefix: :constant, infix: nil, level: :none }, :nil => { prefix: :constant, infix: nil, level: :none }, :bool => { prefix: :constant, infix: nil, level: :none }, :int => { prefix: :constant, infix: nil, level: :none }, @@ -448,7 +449,7 @@ class Parser if @prev.type == :byte error("invalid byte value") if (val < 0 || val > 255) end - Ast::Val.new(Type::Int, nil) + Ast::Val.new(Type::Int, val.to_i) elsif (@prev.type == :string) Ast::Val.new("String", @prev.text) elsif (@prev.type == :nil) @@ -459,6 +460,24 @@ class Parser error("no float support yet") elsif (@prev.type == :char) error("no char support yet") + elsif (@prev.type == "[") + ary = [] + while !matches("]") + ary << expression() + expect(",") if !matches("]") + end + expect("]") + ary + elsif (@prev.type == "{") + hsh = {} + while !matches("}") + name = expect(:ident).text + expect("=") + hsh[name] = expression() + expect(",") if !matches("}") + end + expect("}") + hsh else error("not a valid constant") end diff --git a/example.src b/example.src index b1de7e7..a74f835 100644 --- a/example.src +++ b/example.src @@ -1,7 +1,10 @@ module Main imports (X11, XSel, Posix) -cmd : ?[String] = true +cmd : ?[String] = { + foo = 123, + bar = 321 +} fetchsel(sel : String) {