Skip to content

Instantly share code, notes, and snippets.

@mariusandra
Last active June 17, 2024 08:19
Show Gist options
  • Save mariusandra/6fac950fe73816208587053e121b67e8 to your computer and use it in GitHub Desktop.
Save mariusandra/6fac950fe73816208587053e121b67e8 to your computer and use it in GitHub Desktop.

Hog, the language

This document describes the features of Hog.

General syntax

Comments

// Hog comments start with //
-- You can also use SQL style comments with --
/* or C++ style multi line
blocks */

Variables

// assign 12 to myVar
let myVar := 12 // must use ":=" because "=" is just equals in SQL/HogQL
myVar := 13
myVar := myVar + 1

Comparisons

let myVar := 12
print(myVar = 12 or myVar < 10) // prints true
print(myVar < 12 and myVar > 12) // prints false

let string = 'mystring'
print(string ilike '%str%') // like, ilike, not like, not ilike work

Regex

print('string' =~ 'i.g$') // true
print('string' !~ 'i.g$') // false
print('string' =~* 'I.G$') // true, case insensitive
print('string' !~* 'I.G$') // false, case insensitive

Arrays

let myArray := [1,2,3]
print(myArray.1) // prints 2
print(myArray[1]) // prints 2

Tuples

let myTuple := (1,2,3)
print(myTuple.1) // prints 2
print(myTuple[1]) // prints 2

Objects

let myObject := {'key': 'value'} // must use single quotes !!
print(myTuple.key) // prints 'value'
print(myTuple['key']) // prints 'value'

Strings

let str := 'string'
print(str || ' world') // prints 'string world', SQL concat
print(f'hello {str}') // prints 'hello string'
print(f'hello {f'{str} world'}') // prints 'hello string world'
print(empty(str) ? 'empty' : 'not empty') // prints 'not empty'

Functions

// can only be defined at the top scope
fn addNumbers(num1, num2) { 
    let newNum := num1 + num2
    return newNum
}
print(addNumbers(1, 2))

Logic

let a := 3
if (a > 0) {
    print('yes')
}

print(a > 0 ? 'yes' : 'no') // ternary

Nulls

let a := null
print(a ?? 'is null') // prints 'is null'
print(a ? 'is null' : 'not null') // prints 'is null'
print(empty(a) ? 'is null' : 'not null') // prints 'is null'

While loop

let i := 0
while(i < 3) {
    print(i) // prints 0, 1, 2
    i := i + 1
}

For loop

while(let i := 0; i < 3; i := i + 1) {
    print(i) // prints 0, 1, 2
}

Standard Library

  • concat(...args)
  • match(string, regex)
  • toString(arg)
  • toUUID(arg)
  • toInt(arg)
  • toFloat(arg)
  • ifNull(value, alternative)
  • length(stringOrArray)
  • empty(arg)
  • notEmpty(arg)
  • tuple(...args)
  • lower(str)
  • upper(str)
  • reverse(str)
  • print(...args)
  • jsonParse(str)
  • jsonStringify(obj)
  • base64Encode(str)
  • base64Decode(str)
  • tryBase64Decode(str)
  • encodeURLComponent(str)
  • decodeURLComponent(str)
  • replaceOne(string, needle, replacement)
  • replaceAll(string, needle, replacement)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment