diff --git a/docs.md b/docs.md new file mode 100644 index 0000000000000000000000000000000000000000..3c801f4988a4b1577a74f3c0f93aea8951e17e12 --- /dev/null +++ b/docs.md @@ -0,0 +1,32 @@ +# Documentation for Dhallish! + +*Dhallish* is a ruby gem and provides an executable dhallish. This executable reads from *STDIN* and +outputs *JSON*. The *JSON* output does not aim to be compatible with [dhall-to-json](https://github.com/dhall-lang/dhall-haskell/tree/master/dhall-json). + +*dhallish* accepts the following command line options +- '--pretty': print prettified json +- '--type': print the type of the expression instead of its value + +If passed a file as a command line argument, *dhallish* reads from that file instead of stdin. + +*Dhallish* can also be used from other ruby files, if './lib/dhallish.rb' is required from that file. +The following functions from './lib/dhalish.rb' can be used: + +- `evaluate(dhallcode, ctx=nil, expected_type=nil)`: + Arguments: + - `dhallcode`: A string containing the expression that shall be evaluated + - `ctx`: The context in which the expression is evaluated. If nothing is passed, an empty context is used + - `expected_type`: The Dhallish-Type expected by the caller. If nothing is passed, a list is returned where + the first element is the value of the expression and the second element is the type. + If a Dhallish-Type is passed and the type of the result matches the expected type, only the value is returned. + If the passed type does not match the actual type, a DhallError is raised. + +- `create_ctx(dhallcode, basedir=Dir.pwd)` + Arguments: + - `dhallcode`: A string containing the expression that shall be evaluated + - `basedir`: A string containing a path that is used when files are imported from a relative path + +- `create_ctx_from_file(dhallfile, basedir=nil)` + Arguments + - `dhallfile`: A string containing the path to a file that contains dhallish-code + - `basedir`: A string containing a path that is used when files are imported from a relative path diff --git a/lib/dhallish.rb b/lib/dhallish.rb index 36638412e8fe461f87b88b5a7b4913c8e7f4d41f..f1f3464fca4879765b58420133dfe03b2d85572b 100644 --- a/lib/dhallish.rb +++ b/lib/dhallish.rb @@ -29,17 +29,17 @@ module Dhallish if ctx.nil?; ctx = empty_context() end rawast = @@parser.parse dhallcode - ast = rawast.to_node - - if ast.nil? - raise DhallError, "#{@parser.failure_reason} (line/column: #{@parser.failure_line}:#{@parser.failure_column})" + if rawast.nil? + raise DhallError, "#{@@parser.failure_reason} (line/column: #{@@parser.failure_line}:#{@@parser.failure_column})" end + ast = rawast.to_node + type = ast.compute_type ctx["<#TYPES#>"] res = ast.evaluate ctx["<#VALS#>"] if !expected_type.nil? - if type != expected_type + if !(type == expected_type) raise DhallError, "expression return type missmatch: expected `#{expected_type}`, got: `#{type}`" else res