COMP2212 Report

Table of Contents

1 Main Language Features

1.1 Fundamental Data Types

1.1.1 int

int is used to represent numerical values. There are no doubles/floats etc.

1.1.2 str

str is used to represent String values.

1.2 Arithmetic

All arithmetic uses standard infix notation.

1.2.1 +

1.2.2 -

1.2.3 *

1.2.4 /

1.3 Boolean Logic

The two truth values of logic and Boolean algebra are represented as follows:

1.4 Control Flow

1.4.1 While Loops

1.4.2 if-then-else statements

1.5 Lists

1.5.1 Head and Tail

The main way to alter lists if via two inbuilt functions, head:

and tail:

1.6 Error Handling

Whenever an error is encounter - with IO for example - we generate a exception node in the syntax tree that is executed next. Upon execution a message explaining the error is displayed and the program execution is halted.

2 Syntax

2.1 Scope

Scope in our language is always global – that is, there is no block scope, expression scope, or function scope (functions weren’t implemented).

2.2 Lexical Rules

Most of the syntax style can be seen in section 1. It is important to note that an expression must end with a ; character to denote it is finished, expect if it’s within, for example, the condition part of a while loop.

2.3 Syntax Sugar

Lists are always outputted like this: [1, 2, 3]. This is syntactic sugar; they are actually represented as they are in Haskell: 1:2:3:[].

2.4 Syntax Highlighting

There is syntax highlighting support for our language in Emacs and with Pygments (as seen from the code snippets in this document). Examples of this highlighting, the source for the Emacs Generic Mode and the source for the Pygments lexer are included in the appendix.

3 Execution Model

Our execution model was like that of a CESK machine. We used small step semantics, where during computation we’d leave a “hole” in an expression being evaluated. For example, let our expression be e1 + e2, where e1 is 1 + 2 and e2 is 3:

exp: (1 + 2) + 3  continuation:
exp: 1 + 2        continuation: Hole + 3
exp: 1            continuation: Hole + 2, Hole + 3
val: 1            continuation: Hole + 2, Hole + 3
exp: 2            continuation: 1 + Hole, Hole + 3
val: 2            continuation: 1 + Hole, Hole + 3
val: 3            continuation: Hole + 3
exp: 3            continuation: 3 + Hole
val: 3            continuation: 3 + Hole
val: 6            continuation:

4 Appendix

4.1 BNF Grammar

4.2 Examples of Syntax Highlighting

syntax-highlighting.png

Figure 1: Example of Syntax Highlighting in Emacs with three different themes applied

4.3 gnomecompsci.py

# -*- coding: utf-8 -*-
"""
    pygments.lexers.gnomecompsci
    ~~~~~~~~~~~~~~~~~~~~
    Lexers for Gnome Compsic
"""
import re
from pygments.lexer import RegexLexer, include, bygroups, default, using, \
    this, words, combined
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
    Number, Punctuation, Other
import pygments.unistring as uni

__all__ = ['GnomeCompsciLexer']


class GnomeCompsciLexer(RegexLexer):

    """
    Pygments Lexer for GnomeComspci Files (.spl).
    See https://gnomecompsic.gitlab.com
    """

    name = 'GnomeCompsci'
    aliases = ['gnomecompsci']
    filenames = ['*.spl']
    mimetypes = ['text/spl']

    flags = re.DOTALL | re.UNICODE | re.IGNORECASE | re.MULTILINE

    tokens = {
        'commentsandwhitespace': [
            (r'\s+', Text),
            (r'--.*?\n', Comment.Single),
        ],
        'slashstartsregex': [
            include('commentsandwhitespace'),
            default('#pop')
        ],
        'root': [
            include('commentsandwhitespace'),
            (r'^(?=\s|\/|--)', Text, 'slashstartsregex'),
            (r'[0-9]+', Number.Integer),
            (r'\.\.\.|=>', Punctuation),
            (r'(and|or|==?|!=?|'
             r'[-<>+*/])=?', Operator, 'slashstartsregex'),
            (r'[{(\[;,:]', Punctuation, 'slashstartsregex'),
            (r'[})\].]', Punctuation),
            (r'(for|while|if|then|else)\b', Keyword, 'slashstartsregex'),
            (r'(True|False)\b', Keyword.Constant),
            (r'(readline|print|split|head|tail)\b', Name.Builtin),
            (r'"(\\\\|\\"|[^"])*"', String.Double),
            (r'[$a-zA-Z_][\w.\-:$]*\s*[:=]\s', Name.Variable, 'slashstartsregex'),
            (r'@[$a-zA-Z_][\w.\-:$]*\s*[:=]\s', Name.Variable.Instance, 'slashstartsregex'),
            (r'@', Name.Other, 'slashstartsregex'),
            (r'@?[$a-zA-Z_][\w-]*', Name.Other, 'slashstartsregex')
        ]
    }

4.4 gnome-comspci.el

(defvar gc-constants
  '("True" "False"))
(defvar gc-operators
  '("=" "+" "-" "*" "/" ":" "and" "or" "!"))
(defvar gc-symbs
  '(";" "{" "}" "(" ")" "[" "]"))
(defvar gc-keywords
  '("if" "then" "else" "while"))
(defvar gc-functions
  '("readline" "split" "print" "head" "tail"))

(require 'generic-x)
(define-generic-mode
  'gc-mode                          ;; name of the mode to create
  '("--")                           ;; comments start with '--'
  gc-keywords                        ;; some keywords
  `(( ,(regexp-opt gc-operators) . font-lock-type-face)
    ( ,(regexp-opt gc-symbs) . font-lock-builtin-face)
    ( ,(regexp-opt gc-constants 'words) . font-lock-constant-face)
    ( ,(regexp-opt gc-functions 'words) . font-lock-function-name-face))
  '("\\.spl$")                     ;; files for which to activate this mode 
  nil                              ;; other functions to call
  "A mode for gnome compsci files" ;; doc string for this mode
  )