<commit_before>-- Program State
-- Version: 03/06/2017
module State where

-- Imports
import Data.Functor.Identity
import Text.ParserCombinators.Parsec
import Text.Parsec.Combinator
import Text.Parsec.Prim
import Lexer

-- -----------------------------------------------------------------------------
-- State
-- -----------------------------------------------------------------------------

-- - State
-- [Var]       Memory    (Variables)
-- [Statement] Statments (Functions, Procedures and UserTypes)
type State = ([Var], [Statement])

-- - Search key
-- String  Type name
-- Integer Type scope ID
type Key = (String, Integer)

-- -----------------------------------------------------------------------------
-- Memory
-- -----------------------------------------------------------------------------

-- --------------------------------------------------------
-- Types
-- --------------------------------------------------------

-- Types
data Type =
      AtomicType  String
  	| ArrayType   Integer Type
	| PointerType Type
	| ProcType    ProcDecl
	| FuncType    FuncDecl
	deriving (Eq,Show)

-- Values
data Value =
      NatValue     Int
    | IntValue     Int
    | RealValue    Double
  	| BoolValue    Bool
  	| TextValue    String
    | ArrayValue   [Value]
	| PointerValue Key
	| ProcValue    Key
	| FuncValue    Key
    | UserValue    [Value]
	deriving (Eq,Show)

-- --------------------------------------------------------
-- Variables
-- --------------------------------------------------------


-- - Variable declaration
-- String  Variable name
-- Integer Variable scope ID
-- Type    Variable type
type VarDecl = (String, Integer, Type)

-- - Variable
-- String  Variable name
-- Integer Variable scope ID
-- Type    Variable type
-- Value   Variable value
type Var = (String, Integer, Type, Value)

-- - Updated variable
-- Var  Variable
-- Bool --
type UpdatedVar = (Var, Bool)

-- -----------------------------------------------------------------------------
-- Statements
-- -----------------------------------------------------------------------------

-- --------------------------------------------------------
-- Procedure
-- --------------------------------------------------------

-- --------------------------------------
-- Procedure handler
-- --------------------------------------

-- --------------------------------------------------------
-- Function
-- --------------------------------------------------------

-- --------------------------------------
-- Function handler
-- --------------------------------------

-- --------------------------------------------------------
-- User types
-- --------------------------------------------------------

-- - Field
-- String Variable name<commit_msg>Updates state handler - Adds function and procedure types; - Implements some memory manipulation functions.<commit_after>-- Program State
-- Version: 03/06/2017
module State where

-- Imports
import Data.Functor.Identity
import Text.ParserCombinators.Parsec
import Text.Parsec.Combinator
import Text.Parsec.Prim
import Lexer

-- -----------------------------------------------------------------------------
-- State
-- -----------------------------------------------------------------------------

-- - State
-- [Var]       Memory    (Variables)
-- [Statement] Statments (Functions, Procedures and UserTypes)
type State = ([Var], [Statement])

-- - Search key
-- String  Type name
-- Integer Type scope ID
type Key = (String, Integer)

-- --------------------------------------------------------
-- Parser types
-- --------------------------------------------------------

type Parser      = ParsecT [Token] State Identity
type Interpreter = Parser  (Maybe (Type, Value))
type Statement   = Parser  (Maybe (Maybe (Type, Value)))

-- -----------------------------------------------------------------------------
-- Memory
-- -----------------------------------------------------------------------------

-- --------------------------------------------------------
-- Types
-- --------------------------------------------------------

-- Types
data Type =
      AtomicType  String
  	| ArrayType   Integer Type
	| PointerType Type
	| ProcType    ProcDecl
	| FuncType    FuncDecl
	deriving (Eq, Show)

-- Values
data Value =
      NatValue     Int
    | IntValue     Int
    | RealValue    Double
  	| BoolValue    Bool
  	| TextValue    String
    | ArrayValue   [Value]
	| PointerValue Key
	| ProcValue    Key
	| FuncValue    Key
    | UserValue    [Value]
	deriving (Eq, Show)

-- --------------------------------------------------------
-- Variables
-- --------------------------------------------------------

-- - Variable declaration
-- String  Variable name
-- Integer Variable scope ID
-- Type    Variable type
type VarDecl = (String, Integer, Type)

-- - Variable
-- String  Variable name
-- Integer Variable scope ID
-- Type    Variable type
-- Value   Variable value
type Var = (String, Integer, Type, Value)

-- - Updated variable
-- Var  Variable
-- Bool --
type UpdatedVar = (Var, Bool)

-- --------------------------------------
-- Variable handler
-- --------------------------------------

-- - Insert variable
-- VarDecl Variable declaration
-- State   Current state
-- Return  Updated state
insertVariable :: VarDecl -> State -> State
insertVariable (n, sc, t) (m, st) =
	let nv = (n, sc, t, getInitValue t) in (nv:m, st)

-- - Get variable
-- State  Current state
-- Key    Variable key
-- Return Updated state
getVariable :: State -> Key -> Var
getVariable ((vn, vsc, vt, vv):m, st) (n, sc) =
    if vn == n && vsc == sc then (vn, vsc, vt, vv)
    else getVariable (m, st) (n, sc)  -- Isso remove items da tabela?

-- - Get variable type
-- State  Current state
-- Key    Variable key
-- Return Variable type
getVariableType :: State -> Key -> Type
getVariableType s k = let (_, _, t, _) = getVar s k in t

-- - Update variable

-- -----------------------------------------------------------------------------
-- Statements
-- -----------------------------------------------------------------------------

-- --------------------------------------------------------
-- Procedure
-- --------------------------------------------------------

-- - Procedure declaration
-- String    Procedure name
-- Integer   Procedure scope ID
-- [VarDecl] Parameters
type ProcDec = (String, Integer, [VarDecl])

-- - Procedure
-- String    Procedure name
-- Integer   Procedure scope ID
-- [VarDecl] Parameters
-- [Token]   Statements
type Procedure = (String, Integer, [VarDecl], [Token])

-- --------------------------------------
-- Procedure handler
-- --------------------------------------

-- --------------------------------------------------------
-- Function
-- --------------------------------------------------------

-- - Function declaration
-- String    Function name
-- Integer   Function scope ID
-- [VarDecl] Parameters
-- Type      Return type
type FuncDec = (String, Integer, [VarDecl], Type)

-- - Function
-- String    Function name
-- Integer   Function scope ID
-- [VarDecl] Parameters
-- [Token]   Statements
-- Type      Return type
type Function = (String, Integer, [VarDecl], Type, [Token])

-- --------------------------------------
-- Function handler
-- --------------------------------------

-- --------------------------------------------------------
-- User types
-- --------------------------------------------------------

-- - Field
-- String Variable name
{'fraction_non_alphanumeric': 0.4242465016146394, 'fraction_numerical': 0.002152852529601722, 'mean_word_length': 0.8550037434489643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7279258', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1539, 'n_words': 515}