Skip to content

Instantly share code, notes, and snippets.

@antonijn
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save antonijn/38bdb4f39b6669cbecb2 to your computer and use it in GitHub Desktop.

Select an option

Save antonijn/38bdb4f39b6669cbecb2 to your computer and use it in GitHub Desktop.
Read any C type from a [Token] token stream
-- File parsing hub, providing an interface to the parsers and other
-- helper functions to be used by other parsers.
-- Copyright (C) 2014 Antonie Blom
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
module Compiler.Parser where
import qualified Compiler.Parser.Ast as Ast
import Compiler.Parser.Tokenizer
readType :: [Token] -> Ast.Ast -> (Ast.Type,[Token])
readType toks ast = readType' True toks ast Nothing
readType' :: Bool -> [Token] -> Ast.Ast -> (Maybe Ast.Type) -> (Ast.Type,[Token])
readType' True (("*",_):rst) ast (Just ty) = readType' True rst ast (Just (Ast.Pointer ty))
readType' rt (("int",_):rst) ast _ = readType' rt rst ast (Just Ast.Int)
readType' rt (("char",_):rst) ast _ = readType' rt rst ast (Just Ast.Char)
readType' rt (("short",_):rst) ast _ = readType' rt rst ast (Just Ast.Short)
readType' rt (("long",_):rst) ast _ = readType' rt rst ast (Just Ast.Long)
readType' rt (("double",_):rst) ast _ = readType' rt rst ast (Just Ast.Double)
readType' rt (("float",_):rst) ast _ = readType' rt rst ast (Just Ast.Float)
readType' rt (("void",_):rst) ast _ = readType' rt rst ast (Just Ast.Void)
readType' rt (("unsigned",_):rst) ast (Just ty) = readType' rt rst ast (Just (Ast.Unsigned ty))
readType' rt (("unsigned",_):rst) ast _ =
let (ty,tok) = readType' False rst ast Nothing in readType' rt tok ast (Just (Ast.Unsigned ty))
readType' rt (("signed",_):rst) ast (Just ty) = readType' rt rst ast (Just (Ast.Signed ty))
readType' rt (("signed",_):rst) ast _ =
let (ty,tok) = readType' False rst ast Nothing in readType' rt tok ast (Just (Ast.Signed ty))
readType' rt (("const",_):rst) ast (Just ty) = readType' rt rst ast (Just (Ast.Constant ty))
readType' rt (("const",_):rst) ast _ =
let (ty,tok) = readType' False rst ast Nothing in readType' rt tok ast (Just (Ast.Constant ty))
readType' rt toks _ (Just ty) = (ty,toks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment