Skip to content
Snippets Groups Projects
Commit 5d63bb4c authored by Philipp Panzer's avatar Philipp Panzer
Browse files

fix minor bugs in dhallish.rb

parent 7a781285
Branches
No related tags found
No related merge requests found
docs.md 0 → 100644
# 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
...@@ -29,17 +29,17 @@ module Dhallish ...@@ -29,17 +29,17 @@ module Dhallish
if ctx.nil?; ctx = empty_context() end if ctx.nil?; ctx = empty_context() end
rawast = @@parser.parse dhallcode rawast = @@parser.parse dhallcode
ast = rawast.to_node if rawast.nil?
raise DhallError, "#{@@parser.failure_reason} (line/column: #{@@parser.failure_line}:#{@@parser.failure_column})"
if ast.nil?
raise DhallError, "#{@parser.failure_reason} (line/column: #{@parser.failure_line}:#{@parser.failure_column})"
end end
ast = rawast.to_node
type = ast.compute_type ctx["<#TYPES#>"] type = ast.compute_type ctx["<#TYPES#>"]
res = ast.evaluate ctx["<#VALS#>"] res = ast.evaluate ctx["<#VALS#>"]
if !expected_type.nil? if !expected_type.nil?
if type != expected_type if !(type == expected_type)
raise DhallError, "expression return type missmatch: expected `#{expected_type}`, got: `#{type}`" raise DhallError, "expression return type missmatch: expected `#{expected_type}`, got: `#{type}`"
else else
res res
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment